public void testHandSQL() { Session s = openSession(); Transaction t = s.beginTransaction(); Organization ifa = new Organization( "IFA" ); Organization jboss = new Organization( "JBoss" ); Person gavin = new Person( "Gavin" ); Employment emp = new Employment( gavin, jboss, "AU" ); Serializable orgId = s.save( jboss ); s.save( ifa ); s.save( gavin ); s.save( emp ); t.commit(); t = s.beginTransaction(); Person christian = new Person( "Christian" ); s.save( christian ); Employment emp2 = new Employment( christian, jboss, "EU" ); s.save( emp2 ); t.commit(); s.close(); getSessions().evict( Organization.class ); getSessions().evict( Person.class ); getSessions().evict( Employment.class ); s = openSession(); t = s.beginTransaction(); jboss = ( Organization ) s.get( Organization.class, orgId ); assertEquals( jboss.getEmployments().size(), 2 ); assertEquals( jboss.getName(), "JBOSS" ); emp = ( Employment ) jboss.getEmployments().iterator().next(); gavin = emp.getEmployee(); assertEquals( gavin.getName(), "GAVIN" ); assertEquals( s.getCurrentLockMode( gavin ), LockMode.UPGRADE ); emp.setEndDate( new Date() ); Employment emp3 = new Employment( gavin, jboss, "US" ); s.save( emp3 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Iterator iter = s.getNamedQuery( "allOrganizationsWithEmployees" ).list().iterator(); assertTrue( iter.hasNext() ); Organization o = ( Organization ) iter.next(); assertEquals( o.getEmployments().size(), 3 ); Iterator iter2 = o.getEmployments().iterator(); while ( iter2.hasNext() ) { Employment e = ( Employment ) iter2.next(); s.delete( e ); } iter2 = o.getEmployments().iterator(); while ( iter2.hasNext() ) { Employment e = ( Employment ) iter2.next(); s.delete( e.getEmployee() ); } s.delete( o ); assertFalse( iter.hasNext() ); s.delete( ifa ); t.commit(); s.close(); }
public void testIterateWithEvictBottomOfLoop() { Session s = openSession(); s.beginTransaction(); Set parents = new HashSet(); for (int i=0; i<5; i++) { Parent p = new Parent( String.valueOf( i + 100 ) ); Child child = new Child( "child" + i ); child.setParent( p ); p.getChildren().add( child ); s.save( p ); parents.add(p); } s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); for (Iterator it = s.createQuery( "from Parent" ).iterate(); it.hasNext(); ) { Parent p = (Parent) it.next(); assertEquals( 1, p.getChildren().size() ); s.evict(p); } s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); for (Iterator it=parents.iterator(); it.hasNext(); ) { s.delete(it.next()); } s.getTransaction().commit(); 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/sql/hand/custom/CustomSQLTestSupport.java File path: /hibernate-distribution-3.3.2.GA/project/testsuite/src/test/java/org/hibernate/test/reattachment/ProxyReattachmentTest.java
Method name: void testHandSQL() Method name: void testIterateWithEvictBottomOfLoop()
Number of AST nodes: 3 Number of AST nodes: 2
1
public void testHandSQL() {
1
public void testIterateWithEvictBottomOfLoop() {
2
		Session s = openSession();
2
		Session s = openSession();
3
		Transaction t = s.beginTransaction();
3
		s.beginTransaction();
4
		Organization ifa = new Organization( "IFA" );
4
		Set parents = new HashSet();
5
		Organization jboss = new Organization( "JBoss" );
5
		for (int i=0; i<5; i++) {
6
		Person gavin = new Person( "Gavin" );
6
			Parent p = new Parent( String.valueOf( i + 100 ) );
7
		Employment emp = new Employment( gavin, jboss, "AU" );
7
			Child child = new Child( "child" + i );
8
		Serializable orgId = s.save( jboss );
8
			child.setParent( p );
9
		s.save( ifa );
9
			p.getChildren().add( child );
10
		s.save( gavin );
10
			s.save( p );
11
		s.save( emp );
11
			parents.add(p);
12
		t.commit();
12
		}
13
13
		s.getTransaction().commit();
14
		t = s.beginTransaction();
14
		s.close();
15
		Person christian = new Person( "Christian" );
15
16
		s.save( christian );
16
		s = openSession();
17
		Employment emp2 = new Employment( christian, jboss, "EU" );
17
		s.beginTransaction();
18
		s.save( emp2 );
18
		for (Iterator it = s.createQuery( "from Parent" ).iterate(); it.hasNext(); ) {
19
		t.commit();
19
			Parent p = (Parent) it.next();
20
		s.close();
20
			assertEquals( 1, p.getChildren().size() );
21
21
			s.evict(p);
22
		getSessions().evict( Organization.class );
22
		}
23
		getSessions().evict( Person.class );
23
		s.getTransaction().commit();
24
		getSessions().evict( Employment.class );
24
		s.close();
25
25
26
		s = openSession();
26
		s = openSession();
27
		t = s.beginTransaction();
27
		s.beginTransaction();
28
		jboss = ( Organization ) s.get( Organization.class, orgId );
28
		for (Iterator it=parents.iterator(); it.hasNext(); ) {
29
		assertEquals( jboss.getEmployments().size(), 2 );
29
			s.delete(it.next());
30
		assertEquals( jboss.getName(), "JBOSS" );
30
		}
31
		emp = ( Employment ) jboss.getEmployments().iterator().next();
31
		s.getTransaction().commit();
32
		gavin = emp.getEmployee();
32
		s.close();
33
		assertEquals( gavin.getName(), "GAVIN" );
33
	}
34
		assertEquals( s.getCurrentLockMode( gavin ), LockMode.UPGRADE );
35
		emp.setEndDate( new Date() );
36
		Employment emp3 = new Employment( gavin, jboss, "US" );
37
		s.save( emp3 );
38
		t.commit();
39
		s.close();
40
41
		s = openSession();
42
		t = s.beginTransaction();
43
		Iterator iter = s.getNamedQuery( "allOrganizationsWithEmployees" ).list().iterator();
44
		assertTrue( iter.hasNext() );
45
		Organization o = ( Organization ) iter.next();
46
		assertEquals( o.getEmployments().size(), 3 );
47
		Iterator iter2 = o.getEmployments().iterator();
48
		while ( iter2.hasNext() ) {
49
			Employment e = ( Employment ) iter2.next();
50
			s.delete( e );
51
		}
52
		iter2 = o.getEmployments().iterator();
53
		while ( iter2.hasNext() ) {
54
			Employment e = ( Employment ) iter2.next();
55
			s.delete( e.getEmployee() );
56
		}
57
		s.delete( o );
58
		assertFalse( iter.hasNext() );
59
		s.delete( ifa );
60
		t.commit();
61
		s.close();
62
	}
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements3
    Number of unmapped statements in the first code fragment1
    Number of unmapped statements in the second code fragment1
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.667
    Clone typeType 3
    Mapped Statements
    ID Statement ID Statement
    43
    while (iter2.hasNext())
    43
    while (iter2.hasNext())
    23
    for (Iterator it = parents.iterator(); it.hasNext(); )
    Differences
    Expression1Expression2Difference
    iter2itVARIABLE_NAME_MISMATCH
    java.util.Collectionjava.util.SetVARIABLE_TYPE_MISMATCH
    Preondition Violations
    Type java.util.Collection of variable o.getEmployments() does not match with type java.util.Set of variable parents
    • Make classes java.util.Collection and java.util.Set extend a common superclass
    23
    for (Iterator it = parents.iterator(); it.hasNext(); )
                                                    
    24
    s.delete(it.next());
    Preondition Violations
    Unmatched statement s.delete(it.next()); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    24
    s.delete(it.next());
    44
    Employment e = (Employment)iter2.next();
                                                                                        
    45
    s.delete(e);
    45
    s.delete(e);
    Preondition Violations
    Unmatched statement s.delete(e); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                    
    Precondition Violations (4)
    Row Violation
    1Type java.util.Collection of variable o.getEmployments() does not match with type java.util.Set of variable parents
    2Unmatched statement s.delete(it.next()); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    3Unmatched statement s.delete(e); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    4Clone fragment #1 returns variable e with type org.hibernate.test.sql.hand.Employment , while Clone fragment #2 returns variable it with type java.util.Iterator