KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > test > CallbacksTest


1 //$Id: CallbacksTest.java,v 1.3 2005/07/09 20:04:44 epbernard Exp $
2
package org.hibernate.ejb.test;
3
4 import java.util.Date JavaDoc;
5 import javax.persistence.EntityManager;
6
7 /**
8  * @author Emmanuel Bernard
9  */

10 public class CallbacksTest extends TestCase {
11
12     public void testCallbackMethod() throws Exception JavaDoc {
13         EntityManager em = factory.createEntityManager();
14         Cat c = new Cat();
15         c.setName( "Kitty" );
16         c.setDateOfBirth( new Date JavaDoc( 90, 11, 15 ) );
17         em.getTransaction().begin();
18         em.persist( c );
19         em.getTransaction().commit();
20
21         em.getTransaction().begin();
22         c = em.find( Cat.class, c.getId() );
23         assertFalse( c.getAge() == 0 );
24         c.setName( "Tomcat" ); //update this entity
25
em.getTransaction().commit();
26
27         em.getTransaction().begin();
28         c = em.find( Cat.class, c.getId() );
29         assertEquals( "Tomcat", c.getName() );
30         em.getTransaction().commit();
31         em.close();
32     }
33
34     public void testEntityListener() throws Exception JavaDoc {
35         EntityManager em = factory.createEntityManager();
36         Cat c = new Cat();
37         c.setName( "Kitty" );
38         c.setDateOfBirth( new Date JavaDoc( 90, 11, 15 ) );
39         em.getTransaction().begin();
40         int previousVersion = c.getManualVersion();
41         em.persist( c );
42         em.getTransaction().commit();
43         em.getTransaction().begin();
44         c = em.find( Cat.class, c.getId() );
45         assertNotNull( c.getLastUpdate() );
46         assertTrue( previousVersion < c.getManualVersion() );
47         previousVersion = c.getManualVersion();
48         c.setName( "new name" );
49         em.getTransaction().commit();
50         em.getTransaction().begin();
51         c = em.find( Cat.class, c.getId() );
52         assertTrue( previousVersion < c.getManualVersion() );
53         em.getTransaction().commit();
54
55         em.close();
56     }
57
58     public Class JavaDoc[] getAnnotatedClasses() {
59         return new Class JavaDoc[]{Cat.class};
60     }
61 }
62
Popular Tags