1 package org.hibernate.ejb.test; 3 4 import java.util.Date ; 5 import javax.persistence.EntityManager; 6 7 10 public class CallbacksTest extends TestCase { 11 12 public void testCallbackMethod() throws Exception { 13 EntityManager em = factory.createEntityManager(); 14 Cat c = new Cat(); 15 c.setName( "Kitty" ); 16 c.setDateOfBirth( new Date ( 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" ); 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 { 35 EntityManager em = factory.createEntityManager(); 36 Cat c = new Cat(); 37 c.setName( "Kitty" ); 38 c.setDateOfBirth( new Date ( 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 [] getAnnotatedClasses() { 59 return new Class []{Cat.class}; 60 } 61 } 62 | Popular Tags |