KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > test > callbacks > CallbackAndDirtyTest


1 //$Id: CallbackAndDirtyTest.java,v 1.2 2005/07/21 00:21:58 epbernard Exp $
2
package org.hibernate.ejb.test.callbacks;
3
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import javax.persistence.EntityManager;
7
8 import org.hibernate.ejb.test.TestCase;
9
10 /**
11  * @author Emmanuel Bernard
12  */

13 public class CallbackAndDirtyTest extends TestCase {
14
15     public void testDirtyButNotDirty() throws Exception JavaDoc {
16         EntityManager manager = factory.createEntityManager();
17         manager.getTransaction().begin();
18         Employee mark = new Employee();
19         mark.setName( "Mark" );
20         mark.setTitle( "internal sales" );
21         mark.setSex( 'M' );
22         mark.setAddress( "buckhead" );
23         mark.setZip( "30305" );
24         mark.setCountry( "USA" );
25
26         Customer joe = new Customer();
27         joe.setName( "Joe" );
28         joe.setAddress( "San Francisco" );
29         joe.setZip( "XXXXX" );
30         joe.setCountry( "USA" );
31         joe.setComments( "Very demanding" );
32         joe.setSalesperson( mark );
33
34         Person yomomma = new Person();
35         yomomma.setName( "mum" );
36         yomomma.setSex( 'F' );
37
38         manager.persist( mark );
39         manager.persist( joe );
40         manager.persist( yomomma );
41         long[] ids = {mark.getId(), joe.getId(), yomomma.getId()};
42         manager.getTransaction().commit();
43
44         manager.getTransaction().begin();
45         assertEquals( manager.createQuery( "from Person" ).getResultList().size(), 3 );
46         assertEquals( manager.createQuery( "from Person p where p.class = Customer" ).getResultList().size(), 1 );
47         manager.getTransaction().commit();
48
49         manager.getTransaction().begin();
50         List JavaDoc customers = manager.createQuery( "from Customer c left join fetch c.salesperson" ).getResultList();
51         for ( Iterator JavaDoc iter = customers.iterator() ; iter.hasNext() ; ) {
52             Customer c = (Customer) iter.next();
53             assertEquals( c.getSalesperson().getName(), "Mark" );
54         }
55         assertEquals( customers.size(), 1 );
56         manager.getTransaction().commit();
57
58         manager.getTransaction().begin();
59         customers = manager.createQuery( "from Customer" ).getResultList();
60         for ( Iterator JavaDoc iter = customers.iterator() ; iter.hasNext() ; ) {
61             Customer c = (Customer) iter.next();
62             assertEquals( c.getSalesperson().getName(), "Mark" );
63         }
64         assertEquals( customers.size(), 1 );
65         manager.getTransaction().commit();
66
67         manager.getTransaction().begin();
68         mark = manager.find( Employee.class, new Long JavaDoc( ids[0] ) );
69         joe = (Customer) manager.find( Customer.class, new Long JavaDoc( ids[1] ) );
70         yomomma = manager.find( Person.class, new Long JavaDoc( ids[2] ) );
71
72         mark.setZip( "30306" );
73         assertEquals( 1, manager.createQuery( "from Person p where p.zip = '30306'" ).getResultList().size() );
74         manager.remove( mark );
75         manager.remove( joe );
76         manager.remove( yomomma );
77         assertTrue( manager.createQuery( "from Person" ).getResultList().isEmpty() );
78         manager.getTransaction().commit();
79         manager.close();
80     }
81
82     public Class JavaDoc[] getAnnotatedClasses() {
83         return new Class JavaDoc[]{
84             Customer.class,
85             Employee.class,
86             Person.class
87         };
88     }
89 }
90
Popular Tags