Session s = openSession(); s.beginTransaction(); A a = new A(); a.setData( "Anna" ); s.save( a ); s.getTransaction().commit(); s.close(); // modify detached entity modifyEntity( a ); s = openSession(); s.beginTransaction(); a = ( A ) s.merge( a ); s.getTransaction().commit(); s.close(); verifyModifications( a.getId() ); // change the many-to-one association from h to be a new (transient) A // there is no cascade from H to A, so this should fail when merged assertEquals( 1, a.getHs().size() ); H h = ( H ) a.getHs().iterator().next(); a.getHs().remove( h ); A aNew = new A(); aNew.setData( "Alice" ); aNew.addH( h ); s = openSession(); s.beginTransaction(); try { s.merge( a ); s.merge( h ); fail( "should have thrown TransientObjectException" ); } catch ( TransientObjectException ex ) { // expected } finally { s.getTransaction().rollback(); } s.close();
Session s = openSession(); s.beginTransaction(); A a = new A(); a.setData( "Anna" ); s.save( a ); s.getTransaction().commit(); s.close(); // modify detached entity modifyEntity( a ); s = openSession(); s.beginTransaction(); a = ( A ) s.merge( a ); s.getTransaction().commit(); s.close(); verifyModifications( a.getId() ); // add a new (transient) G to collection in h // there is no cascade from H to the collection, so this should fail when merged assertEquals( 1, a.getHs().size() ); H h = ( H ) a.getHs().iterator().next(); G gNew = new G(); gNew.setData( "Gail" ); gNew.getHs().add( h ); h.getGs().add( gNew ); s = openSession(); s.beginTransaction(); try { s.merge( a ); s.merge( h ); fail( "should have thrown TransientObjectException" ); } catch ( TransientObjectException ex ) { // expected } finally { s.getTransaction().rollback(); } s.close();
Clone fragments detected by clone detection tool
File path: /hibernate-distribution-3.3.2.GA/project/testsuite/src/test/java/org/hibernate/test/cascade/MultiPathCascadeTest.java File path: /hibernate-distribution-3.3.2.GA/project/testsuite/src/test/java/org/hibernate/test/cascade/MultiPathCascadeTest.java
Method name: void testMultiPathMergeNonCascadedTransientEntityInManyToOne() Method name: void testMultiPathMergeNonCascadedTransientEntityInCollection()
Number of AST nodes: 27 Number of AST nodes: 27
1
Session s = openSession();
1
Session s = openSession();
2
		s.beginTransaction();
2
		s.beginTransaction();
3
		A a = new A();
3
		A a = new A();
4
		a.setData( "Anna" );
4
		a.setData( "Anna" );
5
		s.save( a );
5
		s.save( a );
6
		s.getTransaction().commit();
6
		s.getTransaction().commit();
7
		s.close();
7
		s.close();
8
		// modify detached entity
8
		// modify detached entity
9
		modifyEntity( a );
9
		modifyEntity( a );
10
		s = openSession();
10
		s = openSession();
11
		s.beginTransaction();
11
		s.beginTransaction();
12
		a = ( A ) s.merge( a );
12
		a = ( A ) s.merge( a );
13
		s.getTransaction().commit();
13
		s.getTransaction().commit();
14
		s.close();
14
		s.close();
15
		verifyModifications( a.getId() );
15
		verifyModifications( a.getId() );
16
		// change the many-to-one association from h to be a new (transient) A
16
		// add a new (transient) G to collection in h
17
		// there is no cascade from H to A, so this should fail when merged
17
		// there is no cascade from H to the collection, so this should fail when merged
18
		assertEquals( 1, a.getHs().size() );
18
		assertEquals( 1, a.getHs().size() );
19
		H h = ( H ) a.getHs().iterator().next();
19
		H h = ( H ) a.getHs().iterator().next();
20
		a.getHs().remove( h );
20
		
21
		A aNew = new A();
21
G gNew = new G();
22
		aNew.setData( "Alice" );
22
		gNew.setData( "Gail" );
23
		aNew.addH( h );
23
		gNew.getHs().add( h );
24
		h.getGs().add( gNew );
24
		s = openSession();
25
		s = openSession();
25
		s.beginTransaction();
26
		s.beginTransaction();
26
		try {
27
		try {
27
			s.merge( a );
28
			s.merge( a );
28
			s.merge( h );
29
			s.merge( h );
29
			fail( "should have thrown TransientObjectException" );
30
			fail( "should have thrown TransientObjectException" );
30
		}
31
		}
31
		catch ( TransientObjectException ex ) {
32
		catch ( TransientObjectException ex ) {
32
			// expected
33
			// expected
33
		}
34
		}
34
		finally {
35
		finally {
35
			s.getTransaction().rollback();
36
			s.getTransaction().rollback();
36
		}
37
		}
37
		s.close();
38
		s.close();
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.2
Clones locationClones are declared in the same class
Number of node comparisons157
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements23
    Number of unmapped statements in the first code fragment4
    Number of unmapped statements in the second code fragment4
    Time elapsed for statement mapping (ms)10.2
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    1
    Session s = openSession();
    1
    Session s = openSession();
    2
    s.beginTransaction();
    2
    s.beginTransaction();
    3
    A a = new A();
    3
    A a = new A();
    4
    a.setData("Anna");
    4
    a.setData("Anna");
    5
    s.save(a);
    5
    s.save(a);
    6
    s.getTransaction().commit();
    6
    s.getTransaction().commit();
    7
    s.close();
    7
    s.close();
    8
    modifyEntity(a);
    8
    modifyEntity(a);
    9
    s = openSession();
    9
    s = openSession();
    10
    s.beginTransaction();
    10
    s.beginTransaction();
    11
    a = (A)s.merge(a);
    11
    a = (A)s.merge(a);
    12
    s.getTransaction().commit();
    12
    s.getTransaction().commit();
    13
    s.close();
    13
    s.close();
    14
    verifyModifications(a.getId());
    14
    verifyModifications(a.getId());
    15
    assertEquals(1, a.getHs().size());
    15
    assertEquals(1, a.getHs().size());
    16
    H h = (H)a.getHs().iterator().next();
    16
    H h = (H)a.getHs().iterator().next();
                                          
    17
    G gNew = new G();
    17
    a.getHs().remove(h);
                                                    
                                                      
    18
    gNew.setData("Gail");
    18
    A aNew = new A();
                                          
                                                    
    19
    gNew.getHs().add(h);
    19
    aNew.setData("Alice");
                                                        
                                                    
    20
    h.getGs().add(gNew);
    20
    aNew.addH(h);
                                      
    21
    s = openSession();
    21
    s = openSession();
    22
    s.beginTransaction();
    22
    s.beginTransaction();
    23
    try
    23
    try
    24
    s.merge(a);
    24
    s.merge(a);
    25
    s.merge(h);
    25
    s.merge(h);
    26
    fail("should have thrown TransientObjectException");
    26
    fail("should have thrown TransientObjectException");
    27
    s.close();
    27
    s.close();
    Precondition Violations (1)
    Row Violation
    1Clone fragment #1 returns variables a, h , while Clone fragment #2 returns variables h