use of org.hibernate.jpa.test.Cat in project hibernate-orm by hibernate.
the class CallbacksTest method testPostUpdateCollection.
@Test
@FailureExpected(message = "collection change does not trigger an event", jiraKey = "EJB-288")
public void testPostUpdateCollection() throws Exception {
// create a cat
EntityManager em = getOrCreateEntityManager();
Cat cat = new Cat();
em.getTransaction().begin();
cat.setLength(23);
cat.setAge(2);
cat.setName("Beetle");
cat.setDateOfBirth(new Date());
em.persist(cat);
em.getTransaction().commit();
// assert it is persisted
List ids = Cat.getIdList();
Object id = Cat.getIdList().get(ids.size() - 1);
assertNotNull(id);
// add a kitten to the cat - triggers PostCollectionRecreateEvent
int postVersion = Cat.postVersion;
em.getTransaction().begin();
Kitten kitty = new Kitten();
kitty.setName("kitty");
List kittens = new ArrayList<Kitten>();
kittens.add(kitty);
cat.setKittens(kittens);
em.getTransaction().commit();
assertEquals("Post version should have been incremented.", postVersion + 1, Cat.postVersion);
// add another kitten - triggers PostCollectionUpdateEvent.
postVersion = Cat.postVersion;
em.getTransaction().begin();
Kitten tom = new Kitten();
tom.setName("Tom");
cat.getKittens().add(tom);
em.getTransaction().commit();
assertEquals("Post version should have been incremented.", postVersion + 1, Cat.postVersion);
// delete a kitty - triggers PostCollectionUpdateEvent
postVersion = Cat.postVersion;
em.getTransaction().begin();
cat.getKittens().remove(tom);
em.getTransaction().commit();
assertEquals("Post version should have been incremented.", postVersion + 1, Cat.postVersion);
// delete and recreate kittens - triggers PostCollectionRemoveEvent and PostCollectionRecreateEvent)
postVersion = Cat.postVersion;
em.getTransaction().begin();
cat.setKittens(new ArrayList<Kitten>());
em.getTransaction().commit();
assertEquals("Post version should have been incremented.", postVersion + 2, Cat.postVersion);
em.close();
}
use of org.hibernate.jpa.test.Cat in project hibernate-orm by hibernate.
the class EntityManagerFactorySerializationTest method testSerialization.
@Test
public void testSerialization() throws Exception {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(stream);
out.writeObject(entityManagerFactory());
out.close();
byte[] serialized = stream.toByteArray();
stream.close();
ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
ObjectInputStream in = new ObjectInputStream(byteIn);
EntityManagerFactory serializedFactory = (EntityManagerFactory) in.readObject();
in.close();
byteIn.close();
EntityManager em = serializedFactory.createEntityManager();
//em.getTransaction().begin();
//em.setFlushMode( FlushModeType.NEVER );
Cat cat = new Cat();
cat.setAge(3);
cat.setDateOfBirth(new Date());
cat.setLength(22);
cat.setName("Kitty");
em.persist(cat);
Item item = new Item();
item.setName("Train Ticket");
item.setDescr("Paris-London");
em.persist(item);
//em.getTransaction().commit();
//em.getTransaction().begin();
item.setDescr("Paris-Bruxelles");
//em.getTransaction().commit();
//fake the in container work
((HibernateEntityManager) em).getSession().disconnect();
stream = new ByteArrayOutputStream();
out = new ObjectOutputStream(stream);
out.writeObject(em);
out.close();
serialized = stream.toByteArray();
stream.close();
byteIn = new ByteArrayInputStream(serialized);
in = new ObjectInputStream(byteIn);
em = (EntityManager) in.readObject();
in.close();
byteIn.close();
//fake the in container work
em.getTransaction().begin();
item = em.find(Item.class, item.getName());
item.setDescr(item.getDescr() + "-Amsterdam");
cat = (Cat) em.createQuery("select c from " + Cat.class.getName() + " c").getSingleResult();
cat.setLength(34);
em.flush();
em.remove(item);
em.remove(cat);
em.flush();
em.getTransaction().commit();
em.close();
}
use of org.hibernate.jpa.test.Cat in project hibernate-orm by hibernate.
the class EntityManagerSerializationTest method testSerialization.
@Test
public void testSerialization() throws Exception {
EntityManager em = entityManagerFactory().createEntityManager();
//em.getTransaction().begin();
//em.setFlushMode( FlushModeType.NEVER );
Cat cat = new Cat();
cat.setAge(3);
cat.setDateOfBirth(new Date());
cat.setLength(22);
cat.setName("Kitty");
em.persist(cat);
Item item = new Item();
item.setName("Train Ticket");
item.setDescr("Paris-London");
em.persist(item);
//em.getTransaction().commit();
//em.getTransaction().begin();
item.setDescr("Paris-Bruxelles");
//em.getTransaction().commit();
//fake the in container work
((HibernateEntityManager) em).getSession().disconnect();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(stream);
out.writeObject(em);
out.close();
byte[] serialized = stream.toByteArray();
stream.close();
ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
ObjectInputStream in = new ObjectInputStream(byteIn);
em = (EntityManager) in.readObject();
in.close();
byteIn.close();
//fake the in container work
em.getTransaction().begin();
item = em.find(Item.class, item.getName());
item.setDescr(item.getDescr() + "-Amsterdam");
cat = (Cat) em.createQuery("select c from " + Cat.class.getName() + " c").getSingleResult();
cat.setLength(34);
em.flush();
em.remove(item);
em.remove(cat);
em.flush();
em.getTransaction().commit();
em.close();
}
use of org.hibernate.jpa.test.Cat in project hibernate-orm by hibernate.
the class CallbacksTest method testCallbackMethod.
@Test
public void testCallbackMethod() throws Exception {
EntityManager em = getOrCreateEntityManager();
Cat c = new Cat();
c.setName("Kitty");
c.setDateOfBirth(new Date(90, 11, 15));
em.getTransaction().begin();
em.persist(c);
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();
c = em.find(Cat.class, c.getId());
assertFalse(c.getAge() == 0);
//update this entity
c.setName("Tomcat");
em.getTransaction().commit();
em.clear();
em.getTransaction().begin();
c = em.find(Cat.class, c.getId());
assertEquals("Tomcat", c.getName());
em.getTransaction().commit();
em.close();
}
use of org.hibernate.jpa.test.Cat in project hibernate-orm by hibernate.
the class CallbacksTest method testEntityListener.
@Test
public void testEntityListener() throws Exception {
EntityManager em = getOrCreateEntityManager();
Cat c = new Cat();
c.setName("Kitty");
c.setLength(12);
c.setDateOfBirth(new Date(90, 11, 15));
em.getTransaction().begin();
int previousVersion = c.getManualVersion();
em.persist(c);
em.getTransaction().commit();
em.getTransaction().begin();
c = em.find(Cat.class, c.getId());
assertNotNull(c.getLastUpdate());
assertTrue(previousVersion < c.getManualVersion());
assertEquals(12, c.getLength());
previousVersion = c.getManualVersion();
c.setName("new name");
em.getTransaction().commit();
em.getTransaction().begin();
c = em.find(Cat.class, c.getId());
assertTrue(previousVersion < c.getManualVersion());
em.getTransaction().commit();
em.close();
}
Aggregations