Search in sources :

Example 1 with Kitten

use of org.hibernate.jpa.test.Kitten 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)

Aggregations

ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 EntityManager (javax.persistence.EntityManager)1 Cat (org.hibernate.jpa.test.Cat)1 Kitten (org.hibernate.jpa.test.Kitten)1 FailureExpected (org.hibernate.testing.FailureExpected)1 Test (org.junit.Test)1