naturalidtest.java

来自「hibernate-3.0.5 中文文档」· Java 代码 · 共 99 行

JAVA
99
字号
//$Id: NaturalIdTest.java,v 1.1 2005/05/25 01:24:22 oneovthafew Exp $package org.hibernate.test.naturalid;import junit.framework.Test;import junit.framework.TestSuite;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.cfg.Environment;import org.hibernate.criterion.Restrictions;import org.hibernate.test.TestCase;/** * @author Gavin King */public class NaturalIdTest extends TestCase {		public NaturalIdTest(String str) {		super(str);	}		public void testNaturalIdCache() {		Session s = openSession();		Transaction t = s.beginTransaction();				User u = new User("gavin", "hb", "secret");		s.persist(u);				t.commit();		s.close();				getSessions().getStatistics().clear();		s = openSession();		t = s.beginTransaction();				s.createCriteria(User.class)			.add( Restrictions.naturalId()				.set("name", "gavin")				.set("org", "hb") 			)			.setCacheable(true)			.uniqueResult();				t.commit();		s.close();		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );		assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );				s = openSession();		t = s.beginTransaction();				User v = new User("xam", "hb", "foobar");		s.persist(v);				t.commit();		s.close();				getSessions().getStatistics().clear();		s = openSession();		t = s.beginTransaction();				s.createCriteria(User.class)			.add( Restrictions.naturalId()				.set("name", "gavin")				.set("org", "hb") 			).setCacheable(true)			.uniqueResult();				t.commit();		s.close();				assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );		assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );	}		protected void configure(Configuration cfg) {		cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");		cfg.setProperty(Environment.USE_QUERY_CACHE, "true");		cfg.setProperty(Environment.GENERATE_STATISTICS, "true");	}	protected String[] getMappings() {		return new String[] { "naturalid/User.hbm.xml" };	}	public static Test suite() {		return new TestSuite(NaturalIdTest.class);	}	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?