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();
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();
Clone fragments detected by clone detection tool
File path: /hibernate-distribution-3.3.2.GA/project/testsuite/src/test/java/org/hibernate/test/ops/MergeTest.java File path: /hibernate-distribution-3.3.2.GA/project/testsuite/src/test/java/org/hibernate/test/ops/MergeTest.java
Method name: void testMergeBidiForeignKeyOneToOne() Method name: void testNoExtraUpdatesOnMergeVersioned()
Number of AST nodes: 24 Number of AST nodes: 24
1
Session s = openSession();
1
Session s = openSession();
2
        s.beginTransaction();
2
        s.beginTransaction();
3
		Person p = new Person( "steve" );
3
		VersionedEntity entity = new Version
4
		Address a = new Address( "123 Main", "Austin", "US", p );
5
		s.persist( a );
6
		s.persist( p );
7
		s.getTransaction().commit();
8
		s.close();
9
		clearCounts();
10
		p.getAddress().setStreetAddress( "321 Main" );
4
edEntity( "entity", "entity" );
5
		s.persist( entity );
6
		s.getTransaction().commit();
7
		s.close();
8
		clearCounts();
9
		// entity is now detached, but we have made no changes.  so attempt to merge it
10
		// into this new session; this should cause no updates...
11
		s = openSession();
11
		s = openSession();
12
        s.beginTransaction();
12
		s.beginTransaction();
13
		p = ( Person ) s.merge( p );
13
		VersionedEntity mergedEntity = ( VersionedEntity ) s.merge( entity );
14
		s.getTransaction().commit();
14
		s.getTransaction().commit();
15
		s.close();
15
		s.close();
16
		assertInsertCount( 0 );
16
		assert
17
		assertUpdateCount( 0 ); // no cascade
18
		assertDeleteCount( 0
17
UpdateCount( 0 );
18
		assertInsertCount( 0 );
19
        assertEquals( "unexpected version increment", entity.getVersion(), mergedEntity.getVersion() );
20
		///////////////////////////////////////////////////////////////////////
21
		// as a control measure, now update the node while it is detached and
22
		// make sure we get an update as a result...
19
 );
23
		entity.setName( "new name" );
20
		s = openSession();
24
		s = openSession();
21
        s.beginTransaction();
25
		s.beginTransaction();
22
		s.delete( a );
26
		
23
		s.delete( p );
27
entity = ( VersionedEntity ) s.merge( entity );
24
		s.getTransaction().commit();
28
		s.getTransaction().commit();
25
		s.close();
29
		s.close();
30
		assertUpdateCount( 1 );
31
		assertInsertCount( 0 );
32
		///////////////////////////////////////////////////////////////////////
33
		cleanup();
Summary
Number of common nesting structure subtrees1
Number of refactorable cases0
Number of non-refactorable cases1
Time elapsed for finding largest common nesting structure subtrees (ms)0.0
Clones locationClones are declared in the same class
Number of node comparisons200
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements17
    Number of unmapped statements in the first code fragment7
    Number of unmapped statements in the second code fragment7
    Time elapsed for statement mapping (ms)0.0
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    1
    Session s = openSession();
    1
    Session s = openSession();
    2
    s.beginTransaction();
    2
    s.beginTransaction();
                                                                                                                                      
    3
    VersionedEntity entity = new VersionedEntity("entity", "entity");
    Preondition Violations
    Unmatched statement VersionedEntity entity=new VersionedEntity("entity","entity"); cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
    3
    VersionedEntity entity = new VersionedEntity("entity", "entity");
    3
    Person p = new Person("steve");
    3
    Person p = new Person("steve");
    Preondition Violations
    Unmatched statement Person p=new Person("steve"); cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
                                                                    
    4
    Address a = new Address("123 Main", "Austin", "US", p);
    4
    Address a = new Address("123 Main", "Austin", "US", p);
    Preondition Violations
    Unmatched statement Address a=new Address("123 Main","Austin","US",p); cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
                                                                                                              
    5
    s.persist(a);
    5
    s.persist(a);
    4
    s.persist(entity);
    Differences
    Expression1Expression2Difference
    aentityVARIABLE_NAME_MISMATCH
    org.hibernate.test.ops.Addressorg.hibernate.test.ops.VersionedEntityVARIABLE_TYPE_MISMATCH
    Preondition Violations
    Type org.hibernate.test.ops.Address of variable a does not match with type org.hibernate.test.ops.VersionedEntity of variable entity
    • Make classes org.hibernate.test.ops.Address and org.hibernate.test.ops.VersionedEntity extend a common superclass
    4
    s.persist(entity);
    6
    s.persist(p);
    6
    s.persist(p);
    Preondition Violations
    Unmatched statement s.persist(p); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                    
    7
    s.getTransaction().commit();
    5
    s.getTransaction().commit();
    8
    s.close();
    6
    s.close();
    9
    clearCounts();
    7
    clearCounts();
    10
    p.getAddress().setStreetAddress("321 Main");
                                                                                                    
    11
    s = openSession();
    8
    s = openSession();
    12
    s.beginTransaction();
    9
    s.beginTransaction();
                                                                                                                                        
    10
    VersionedEntity mergedEntity = (VersionedEntity)s.merge(entity);
    Preondition Violations
    Unmatched statement VersionedEntity mergedEntity=(VersionedEntity)s.merge(entity); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    10
    VersionedEntity mergedEntity = (VersionedEntity)s.merge(entity);
    13
    p = (Person)s.merge(p);
    13
    p = (Person)s.merge(p);
    Preondition Violations
    Unmatched statement p=(Person)s.merge(p); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                                      
    14
    s.getTransaction().commit();
    11
    s.getTransaction().commit();
    15
    s.close();
    12
    s.close();
    16
    assertInsertCount(0);
    14
    assertInsertCount(0);
    17
    assertUpdateCount(0);
    13
    assertUpdateCount(0);
                                                                                                                                                                                                  
    15
    assertEquals("unexpected version increment", entity.getVersion(), mergedEntity.getVersion());
                                                                  
    16
    entity.setName("new name");
    Preondition Violations
    Unmatched statement entity.setName("new name"); cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
    16
    entity.setName("new name");
    18
    assertDeleteCount(0);
    18
    assertDeleteCount(0);
    22
    assertUpdateCount(1);
    Differences
    Expression1Expression2Difference
    01LITERAL_VALUE_MISMATCH
    assertDeleteCountassertUpdateCountMETHOD_INVOCATION_NAME_MISMATCH
    Preondition Violations
    Expression assertDeleteCount(0) is a void method call, and thus it cannot be parameterized
    Expression assertUpdateCount(1) is a void method call, and thus it cannot be parameterized
    22
    assertUpdateCount(1);
    19
    s = openSession();
    17
    s = openSession();
    20
    s.beginTransaction();
    18
    s.beginTransaction();
                                                                                            
    19
    entity = (VersionedEntity)s.merge(entity);
    Preondition Violations
    Unmatched statement entity=(VersionedEntity)s.merge(entity); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    19
    entity = (VersionedEntity)s.merge(entity);
    21
    s.delete(a);
    21
    s.delete(a);
    Preondition Violations
    Unmatched statement s.delete(a); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                    
    22
    s.delete(p);
    22
    s.delete(p);
    Preondition Violations
    Unmatched statement s.delete(p); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                    
    23
    s.getTransaction().commit();
    20
    s.getTransaction().commit();
    24
    s.close();
    21
    s.close();
                                                      
    23
    assertInsertCount(0);
                                
    24
    cleanup();
    Precondition Violations (13)
    Row Violation
    1Unmatched statement VersionedEntity entity=new VersionedEntity("entity","entity"); cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
    2Unmatched statement Person p=new Person("steve"); cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
    3Unmatched statement Address a=new Address("123 Main","Austin","US",p); cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
    4Type org.hibernate.test.ops.Address of variable a does not match with type org.hibernate.test.ops.VersionedEntity of variable entity
    5Unmatched statement s.persist(p); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    6Unmatched statement VersionedEntity mergedEntity=(VersionedEntity)s.merge(entity); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    7Unmatched statement p=(Person)s.merge(p); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    8Unmatched statement entity.setName("new name"); cannot be moved before the extracted code, because it has control dependencies from statements that will be extracted
    9Expression assertDeleteCount(0) is a void method call, and thus it cannot be parameterized
    10Expression assertUpdateCount(1) is a void method call, and thus it cannot be parameterized
    11Unmatched statement entity=(VersionedEntity)s.merge(entity); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    12Unmatched statement s.delete(a); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    13Unmatched statement s.delete(p); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted