/
CallbackAndDirtyTest.java
93 lines (80 loc) · 2.88 KB
/
CallbackAndDirtyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//$Id$
package org.hibernate.ejb.test.callbacks;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import org.hibernate.ejb.test.TestCase;
/**
* @author Emmanuel Bernard
*/
public class CallbackAndDirtyTest extends TestCase {
public void testDirtyButNotDirty() throws Exception {
EntityManager manager = getOrCreateEntityManager();
manager.getTransaction().begin();
Employee mark = new Employee();
mark.setName( "Mark" );
mark.setTitle( "internal sales" );
mark.setSex( 'M' );
mark.setAddress( "buckhead" );
mark.setZip( "30305" );
mark.setCountry( "USA" );
Customer joe = new Customer();
joe.setName( "Joe" );
joe.setSex( 'M' );
joe.setAddress( "San Francisco" );
joe.setZip( "XXXXX" );
joe.setCountry( "USA" );
joe.setComments( "Very demanding" );
joe.setSalesperson( mark );
Person yomomma = new Person();
yomomma.setName( "mum" );
yomomma.setSex( 'F' );
manager.persist( mark );
manager.persist( joe );
manager.persist( yomomma );
long[] ids = {mark.getId(), joe.getId(), yomomma.getId()};
manager.getTransaction().commit();
manager.getTransaction().begin();
assertEquals(
manager.createQuery( "select p.address, p.name from Person p order by p.name" ).getResultList().size(),
3
);
assertEquals( manager.createQuery( "select p from Person p where p.class = Customer" ).getResultList().size(), 1 );
manager.getTransaction().commit();
manager.getTransaction().begin();
List customers = manager.createQuery( "select c from Customer c left join fetch c.salesperson" ).getResultList();
for ( Iterator iter = customers.iterator(); iter.hasNext() ; ) {
Customer c = (Customer) iter.next();
assertEquals( c.getSalesperson().getName(), "Mark" );
}
assertEquals( customers.size(), 1 );
manager.getTransaction().commit();
manager.getTransaction().begin();
customers = manager.createQuery( "select c from Customer c" ).getResultList();
for ( Iterator iter = customers.iterator(); iter.hasNext() ; ) {
Customer c = (Customer) iter.next();
assertEquals( c.getSalesperson().getName(), "Mark" );
}
assertEquals( customers.size(), 1 );
manager.getTransaction().commit();
manager.getTransaction().begin();
mark = manager.find( Employee.class, new Long( ids[0] ) );
joe = (Customer) manager.find( Customer.class, new Long( ids[1] ) );
yomomma = manager.find( Person.class, new Long( ids[2] ) );
mark.setZip( "30306" );
assertEquals( 1, manager.createQuery( "select p from Person p where p.zip = '30306'" ).getResultList().size() );
manager.remove( mark );
manager.remove( joe );
manager.remove( yomomma );
assertTrue( manager.createQuery( "select p from Person p" ).getResultList().isEmpty() );
manager.getTransaction().commit();
manager.close();
}
public Class[] getAnnotatedClasses() {
return new Class[]{
Customer.class,
Employee.class,
Person.class
};
}
}