📄 mergetest.java
字号:
//$Id: MergeTest.java 11037 2007-01-09 16:04:16Z steve.ebersole@jboss.com $package org.hibernate.test.ops;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import junit.framework.Test;import org.hibernate.Hibernate;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.NonUniqueObjectException;import org.hibernate.StaleObjectStateException;import org.hibernate.junit.functional.FunctionalTestClassTestSuite;import org.hibernate.criterion.Projections;/** * @author Gavin King */public class MergeTest extends AbstractOperationTestCase { public MergeTest(String str) { super( str ); } public static Test suite() { return new FunctionalTestClassTestSuite( MergeTest.class ); } public void testMergeStaleVersionFails() throws Exception { Session s = openSession(); s.beginTransaction(); VersionedEntity entity = new VersionedEntity( "entity", "entity" ); s.persist( entity ); s.getTransaction().commit(); s.close(); // make the detached 'entity' reference stale... s = openSession(); s.beginTransaction(); VersionedEntity entity2 = ( VersionedEntity ) s.get( VersionedEntity.class, entity.getId() ); entity2.setName( "entity-name" ); s.getTransaction().commit(); s.close(); // now try to reattch it s = openSession(); s.beginTransaction(); try { s.merge( entity ); s.getTransaction().commit(); fail( "was expecting staleness error" ); } catch ( StaleObjectStateException expected ) { // expected outcome... } finally { s.getTransaction().rollback(); s.close(); } } public void testMergeBidiPrimayKeyOneToOne() throws Exception { Session s = openSession(); s.beginTransaction(); Person p = new Person( "steve" ); new PersonalDetails( "I have big feet", p ); s.persist( p ); s.getTransaction().commit(); s.close(); clearCounts(); p.getDetails().setSomePersonalDetail( p.getDetails().getSomePersonalDetail() + " and big hands too" ); s = openSession(); s.beginTransaction(); p = ( Person ) s.merge( p ); s.getTransaction().commit(); s.close(); assertInsertCount( 0 ); assertUpdateCount( 1 ); assertDeleteCount( 0 ); s = openSession(); s.beginTransaction(); s.delete( p ); s.getTransaction().commit(); s.close(); } public void testMergeBidiForeignKeyOneToOne() throws Exception { Session s = openSession(); s.beginTransaction(); Person p = new Person( "steve" ); Address a = new Address( "123 Main", "Austin", "US", p ); s.persist( a ); s.persist( p ); s.getTransaction().commit(); s.close(); clearCounts(); p.getAddress().setStreetAddress( "321 Main" ); s = openSession(); s.beginTransaction(); p = ( Person ) s.merge( p ); s.getTransaction().commit(); s.close(); assertInsertCount( 0 ); assertUpdateCount( 0 ); // no cascade assertDeleteCount( 0 ); s = openSession(); s.beginTransaction(); s.delete( a ); s.delete( p ); s.getTransaction().commit(); s.close(); } public void testNoExtraUpdatesOnMerge() throws Exception { Session s = openSession(); s.beginTransaction(); Node node = new Node( "test" ); s.persist( node ); s.getTransaction().commit(); s.close(); clearCounts(); // node is now detached, but we have made no changes. so attempt to merge it // into this new session; this should cause no updates... s = openSession(); s.beginTransaction(); node = ( Node ) s.merge( node ); s.getTransaction().commit(); s.close(); assertUpdateCount( 0 ); assertInsertCount( 0 ); /////////////////////////////////////////////////////////////////////// // as a control measure, now update the node while it is detached and // make sure we get an update as a result... node.setDescription( "new description" ); s = openSession(); s.beginTransaction(); node = ( Node ) s.merge( node ); s.getTransaction().commit(); s.close(); assertUpdateCount( 1 ); assertInsertCount( 0 ); /////////////////////////////////////////////////////////////////////// cleanup(); } public void testNoExtraUpdatesOnMergeWithCollection() throws Exception { Session s = openSession(); s.beginTransaction(); Node parent = new Node( "parent" ); Node child = new Node( "child" ); parent.getChildren().add( child ); child.setParent( parent ); s.persist( parent ); s.getTransaction().commit(); s.close(); clearCounts(); // parent is now detached, but we have made no changes. so attempt to merge it // into this new session; this should cause no updates... s = openSession(); s.beginTransaction(); parent = ( Node ) s.merge( parent ); s.getTransaction().commit(); s.close(); assertUpdateCount( 0 ); assertInsertCount( 0 ); /////////////////////////////////////////////////////////////////////// // as a control measure, now update the node while it is detached and // make sure we get an update as a result... ( ( Node ) parent.getChildren().iterator().next() ).setDescription( "child's new description" ); parent.getChildren().add( new Node( "second child" ) ); s = openSession(); s.beginTransaction(); parent = ( Node ) s.merge( parent ); s.getTransaction().commit(); s.close(); assertUpdateCount( 1 ); assertInsertCount( 1 ); /////////////////////////////////////////////////////////////////////// cleanup(); } public void testNoExtraUpdatesOnMergeVersioned() throws Exception { Session s = openSession(); s.beginTransaction(); VersionedEntity entity = new VersionedEntity( "entity", "entity" ); s.persist( entity ); s.getTransaction().commit(); s.close(); clearCounts(); // entity is now detached, but we have made no changes. so attempt to merge it // into this new session; this should cause no updates... s = openSession(); s.beginTransaction(); VersionedEntity mergedEntity = ( VersionedEntity ) s.merge( entity ); s.getTransaction().commit(); s.close(); assertUpdateCount( 0 ); assertInsertCount( 0 ); assertEquals( "unexpected version increment", entity.getVersion(), mergedEntity.getVersion() ); /////////////////////////////////////////////////////////////////////// // as a control measure, now update the node while it is detached and // make sure we get an update as a result... entity.setName( "new name" ); s = openSession(); s.beginTransaction(); entity = ( VersionedEntity ) s.merge( entity ); s.getTransaction().commit(); s.close(); assertUpdateCount( 1 ); assertInsertCount( 0 ); /////////////////////////////////////////////////////////////////////// cleanup(); } public void testNoExtraUpdatesOnMergeVersionedWithCollection() throws Exception { Session s = openSession(); s.beginTransaction(); VersionedEntity parent = new VersionedEntity( "parent", "parent" ); VersionedEntity child = new VersionedEntity( "child", "child" ); parent.getChildren().add( child ); child.setParent( parent ); s.persist( parent ); s.getTransaction().commit(); s.close(); clearCounts(); // parent is now detached, but we have made no changes. so attempt to merge it // into this new session; this should cause no updates... s = openSession(); s.beginTransaction(); VersionedEntity mergedParent = ( VersionedEntity ) s.merge( parent ); s.getTransaction().commit(); s.close(); assertUpdateCount( 0 ); assertInsertCount( 0 ); assertEquals( "unexpected parent version increment", parent.getVersion(), mergedParent.getVersion() ); VersionedEntity mergedChild = ( VersionedEntity ) mergedParent.getChildren().iterator().next(); assertEquals( "unexpected child version increment", child.getVersion(), mergedChild.getVersion() ); /////////////////////////////////////////////////////////////////////// // as a control measure, now update the node while it is detached and // make sure we get an update as a result... mergedParent.setName( "new name" ); mergedParent.getChildren().add( new VersionedEntity( "child2", "new child" ) ); s = openSession(); s.beginTransaction(); parent = ( VersionedEntity ) s.merge( mergedParent ); s.getTransaction().commit(); s.close(); assertUpdateCount( 1 ); assertInsertCount( 1 ); /////////////////////////////////////////////////////////////////////// cleanup(); } public void testPersistThenMergeInSameTxnWithVersion() { Session s = openSession(); Transaction tx = s.beginTransaction(); VersionedEntity entity = new VersionedEntity( "test", "test" ); s.persist( entity ); s.merge( new VersionedEntity( "test", "test-2" ) ); try { // control operation... s.saveOrUpdate( new VersionedEntity( "test", "test-3" ) ); fail( "saveOrUpdate() should fail here" ); } catch( NonUniqueObjectException expected ) { // expected behavior } tx.commit(); s.close(); cleanup(); } public void testPersistThenMergeInSameTxnWithTimestamp() { Session s = openSession(); Transaction tx = s.beginTransaction(); TimestampedEntity entity = new TimestampedEntity( "test", "test" ); s.persist( entity ); s.merge( new TimestampedEntity( "test", "test-2" ) ); try { // control operation... s.saveOrUpdate( new TimestampedEntity( "test", "test-3" ) ); fail( "saveOrUpdate() should fail here" ); } catch( NonUniqueObjectException expected ) { // expected behavior } tx.commit(); s.close(); cleanup(); } public void testMergeDeepTree() { clearCounts(); Session s = openSession(); Transaction tx = s.beginTransaction(); Node root = new Node("root"); Node child = new Node("child"); Node grandchild = new Node("grandchild"); root.addChild(child); child.addChild(grandchild);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -