cmttest.java

来自「介绍了hibernate的入门有一些基本常用的事例」· Java 代码 · 共 93 行

JAVA
93
字号
//$Id: CMTTest.java,v 1.8 2005/04/01 22:55:57 steveebersole Exp $package org.hibernate.test.tm;import java.util.HashMap;import java.util.Map;import junit.framework.Test;import junit.framework.TestSuite;import org.hibernate.EntityMode;import org.hibernate.Session;import org.hibernate.impl.SessionFactoryImpl;import org.hibernate.cfg.Configuration;import org.hibernate.cfg.Environment;import org.hibernate.test.TestCase;/** * @author Gavin King */public class CMTTest extends TestCase {		public CMTTest(String str) {		super(str);	}		public void testCMT() throws Exception {		DummyTransactionManager.INSTANCE.begin();		Session s = openSession();		DummyTransactionManager.INSTANCE.getTransaction().commit();		assertFalse( s.isOpen() );		assertEquals( getSessions().getStatistics().getFlushCount(), 0 );		DummyTransactionManager.INSTANCE.begin();		s = openSession();		DummyTransactionManager.INSTANCE.getTransaction().rollback();		assertFalse( s.isOpen() );		DummyTransactionManager.INSTANCE.begin();		s = openSession();		Map item = new HashMap();		item.put("name", "The Item");		item.put("description", "The only item we have");		s.getSession(EntityMode.MAP).persist("Item", item);		DummyTransactionManager.INSTANCE.getTransaction().commit();		assertFalse( s.isOpen() );		DummyTransactionManager.INSTANCE.begin();		s = openSession().getSession(EntityMode.MAP);		item = (Map) s.createQuery("from Item").uniqueResult();		assertNotNull(item);		s.delete(item);		DummyTransactionManager.INSTANCE.getTransaction().commit();		assertFalse( s.isOpen() );				assertEquals( getSessions().getStatistics().getSuccessfulTransactionCount(), 3 );		assertEquals( getSessions().getStatistics().getEntityDeleteCount(), 1 );		assertEquals( getSessions().getStatistics().getEntityInsertCount(), 1 );		assertEquals( getSessions().getStatistics().getSessionOpenCount(), 6 );		assertEquals( getSessions().getStatistics().getSessionCloseCount(), 6 );		assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );		assertEquals( getSessions().getStatistics().getFlushCount(), 2 );		// Test the new current-session stuff		DummyTransactionManager.INSTANCE.begin();		s = getSessions().getCurrentSession();		Session s2 = getSessions().getCurrentSession();		assertEquals( s, s2 );		DummyTransactionManager.INSTANCE.getTransaction().commit();		assertFalse( s.isOpen() );		// TODO : would be nice to automate-test that the SF internal map actualy gets cleaned up		//      i verified that is does currently in my debugger	}	protected String[] getMappings() {		return new String[] { "tm/Item.hbm.xml" };	}	public static Test suite() {		return new TestSuite(CMTTest.class);	}	protected void configure(Configuration cfg) {		cfg.setProperty(Environment.CONNECTION_PROVIDER, DummyConnectionProvider.class.getName());		cfg.setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, DummyTransactionManagerLookup.class.getName());		cfg.setProperty(Environment.AUTO_CLOSE_SESSION, "true");		cfg.setProperty(Environment.FLUSH_BEFORE_COMPLETION, "true");		cfg.setProperty(Environment.GENERATE_STATISTICS, "true");	}}

⌨️ 快捷键说明

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