Search in sources :

Example 1 with Cat

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();
}
Also used : EntityManager(javax.persistence.EntityManager) Cat(org.hibernate.jpa.test.Cat) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date) Kitten(org.hibernate.jpa.test.Kitten) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 2 with Cat

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();
}
Also used : Item(org.hibernate.jpa.test.Item) HibernateEntityManager(org.hibernate.jpa.HibernateEntityManager) EntityManager(javax.persistence.EntityManager) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityManagerFactory(javax.persistence.EntityManagerFactory) Cat(org.hibernate.jpa.test.Cat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 3 with Cat

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();
}
Also used : Item(org.hibernate.jpa.test.Item) HibernateEntityManager(org.hibernate.jpa.HibernateEntityManager) EntityManager(javax.persistence.EntityManager) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Cat(org.hibernate.jpa.test.Cat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 4 with Cat

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();
}
Also used : EntityManager(javax.persistence.EntityManager) Cat(org.hibernate.jpa.test.Cat) Date(java.util.Date) Test(org.junit.Test)

Example 5 with Cat

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();
}
Also used : EntityManager(javax.persistence.EntityManager) Cat(org.hibernate.jpa.test.Cat) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Date (java.util.Date)6 EntityManager (javax.persistence.EntityManager)6 Cat (org.hibernate.jpa.test.Cat)6 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutput (java.io.ObjectOutput)2 ObjectOutputStream (java.io.ObjectOutputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 HibernateEntityManager (org.hibernate.jpa.HibernateEntityManager)2 Item (org.hibernate.jpa.test.Item)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 Kitten (org.hibernate.jpa.test.Kitten)1 FailureExpected (org.hibernate.testing.FailureExpected)1