Search in sources :

Example 16 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class UnversionedCascadeDereferencedCollectionTest method testGetAndReplaceCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndReplaceCollection() {
    Session s = openSession();
    s.getTransaction().begin();
    UnversionedCascadeOne one = new UnversionedCascadeOne();
    assertNull(one.getManies());
    s.save(one);
    assertNull(one.getManies());
    EntityEntry eeOne = getEntityEntry(s, one);
    assertNull(eeOne.getLoadedValue("manies"));
    s.flush();
    assertNull(one.getManies());
    assertNull(eeOne.getLoadedValue("manies"));
    s.getTransaction().commit();
    s.close();
    final String role = UnversionedCascadeOne.class.getName() + ".manies";
    s = openSession();
    s.getTransaction().begin();
    one = (UnversionedCascadeOne) s.get(UnversionedCascadeOne.class, one.getId());
    // When returned by Session.get(), one.getManies() will return a PersistentCollection;
    // the EntityEntry loaded state should contain the same PersistentCollection.
    eeOne = getEntityEntry(s, one);
    assertNotNull(one.getManies());
    AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue("manies");
    assertSame(one.getManies(), maniesEEOneStateOrig);
    // Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
    assertEquals(role, maniesEEOneStateOrig.getRole());
    assertEquals(one.getId(), maniesEEOneStateOrig.getKey());
    assertSame(s, maniesEEOneStateOrig.getSession());
    // Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
    CollectionEntry ceManiesOrig = getCollectionEntry(s, maniesEEOneStateOrig);
    assertNotNull(ceManiesOrig);
    assertEquals(role, ceManiesOrig.getRole());
    assertSame(sessionFactory().getCollectionPersister(role), ceManiesOrig.getLoadedPersister());
    assertEquals(one.getId(), ceManiesOrig.getKey());
    // replace collection
    one.setManies(new HashSet<Many>());
    s.flush();
    // Ensure the same EntityEntry is being used.
    assertSame(eeOne, getEntityEntry(s, one));
    // Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
    assertNull(getCollectionEntry(s, maniesEEOneStateOrig));
    // Ensure the original CollectionEntry has role, persister, and key set to null.
    assertNull(ceManiesOrig.getRole());
    assertNull(ceManiesOrig.getLoadedPersister());
    assertNull(ceManiesOrig.getKey());
    // Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
    // has key and role set to null.
    assertNull(maniesEEOneStateOrig.getKey());
    assertNull(maniesEEOneStateOrig.getRole());
    // one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
    assertTrue(PersistentCollection.class.isInstance(one.getManies()));
    assertEquals(role, ((PersistentCollection) one.getManies()).getRole());
    assertEquals(one.getId(), ((PersistentCollection) one.getManies()).getKey());
    assertSame(s, ((AbstractPersistentCollection) one.getManies()).getSession());
    // Ensure eeOne.getLoadedState() contains the new collection.
    assertSame(one.getManies(), eeOne.getLoadedValue("manies"));
    // Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
    CollectionEntry ceManiesAfterReplace = getCollectionEntry(s, (PersistentCollection) one.getManies());
    assertNotNull(ceManiesAfterReplace);
    assertEquals(role, ceManiesAfterReplace.getRole());
    assertSame(sessionFactory().getCollectionPersister(role), ceManiesAfterReplace.getLoadedPersister());
    assertEquals(one.getId(), ceManiesAfterReplace.getKey());
    // Ensure the session in maniesEEOneStateOrig has been unset.
    assertNull(maniesEEOneStateOrig.getSession());
    s.getTransaction().commit();
    s.close();
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) EntityEntry(org.hibernate.engine.spi.EntityEntry) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 17 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class VersionedNoCascadeDereferencedCollectionTest method testGetAndReplaceCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndReplaceCollection() {
    Session s = openSession();
    s.getTransaction().begin();
    VersionedNoCascadeOne one = new VersionedNoCascadeOne();
    assertNull(one.getManies());
    s.save(one);
    assertNull(one.getManies());
    EntityEntry eeOne = getEntityEntry(s, one);
    assertNull(eeOne.getLoadedValue("manies"));
    s.flush();
    assertNull(one.getManies());
    assertNull(eeOne.getLoadedValue("manies"));
    s.getTransaction().commit();
    s.close();
    final String role = VersionedNoCascadeOne.class.getName() + ".manies";
    s = openSession();
    s.getTransaction().begin();
    one = (VersionedNoCascadeOne) s.get(VersionedNoCascadeOne.class, one.getId());
    // When returned by Session.get(), one.getManies() will return a PersistentCollection;
    // the EntityEntry loaded state should contain the same PersistentCollection.
    eeOne = getEntityEntry(s, one);
    assertNotNull(one.getManies());
    AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue("manies");
    assertSame(one.getManies(), maniesEEOneStateOrig);
    // Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
    assertEquals(role, maniesEEOneStateOrig.getRole());
    assertEquals(one.getId(), maniesEEOneStateOrig.getKey());
    assertSame(s, maniesEEOneStateOrig.getSession());
    // Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
    CollectionEntry ceManiesOrig = getCollectionEntry(s, maniesEEOneStateOrig);
    assertNotNull(ceManiesOrig);
    assertEquals(role, ceManiesOrig.getRole());
    assertSame(sessionFactory().getCollectionPersister(role), ceManiesOrig.getLoadedPersister());
    assertEquals(one.getId(), ceManiesOrig.getKey());
    // replace collection
    one.setManies(new HashSet<Many>());
    s.flush();
    // Ensure the same EntityEntry is being used.
    assertSame(eeOne, getEntityEntry(s, one));
    // Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
    assertNull(getCollectionEntry(s, maniesEEOneStateOrig));
    // Ensure the original CollectionEntry has role, persister, and key set to null.
    assertNull(ceManiesOrig.getRole());
    assertNull(ceManiesOrig.getLoadedPersister());
    assertNull(ceManiesOrig.getKey());
    // Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
    // has key and role set to null.
    assertNull(maniesEEOneStateOrig.getKey());
    assertNull(maniesEEOneStateOrig.getRole());
    // one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
    assertTrue(PersistentCollection.class.isInstance(one.getManies()));
    assertEquals(role, ((PersistentCollection) one.getManies()).getRole());
    assertEquals(one.getId(), ((PersistentCollection) one.getManies()).getKey());
    assertSame(s, ((AbstractPersistentCollection) one.getManies()).getSession());
    // Ensure eeOne.getLoadedState() contains the new collection.
    assertSame(one.getManies(), eeOne.getLoadedValue("manies"));
    // Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
    CollectionEntry ceManiesAfterReplace = getCollectionEntry(s, (PersistentCollection) one.getManies());
    assertNotNull(ceManiesAfterReplace);
    assertEquals(role, ceManiesAfterReplace.getRole());
    assertSame(sessionFactory().getCollectionPersister(role), ceManiesAfterReplace.getLoadedPersister());
    assertEquals(one.getId(), ceManiesAfterReplace.getKey());
    // Ensure the session in maniesEEOneStateOrig has been unset.
    assertNull(maniesEEOneStateOrig.getSession());
    s.getTransaction().commit();
    s.close();
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) EntityEntry(org.hibernate.engine.spi.EntityEntry) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 18 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testUpdateParentNullToOneChild.

@Test
public void testUpdateParentNullToOneChild() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithNullChildren("parent");
    listeners.clear();
    assertNull(parent.getChildren());
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    assertNotNull(parent.getChildren());
    Child newChild = parent.addChild("new");
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) parent.getChildren()).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, index++);
    }
    checkResult(listeners, listeners.getPreCollectionUpdateListener(), parent, index++);
    checkResult(listeners, listeners.getPostCollectionUpdateListener(), parent, index++);
    if (newChild instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getPreCollectionRecreateListener(), (ChildWithBidirectionalManyToMany) newChild, index++);
        checkResult(listeners, listeners.getPostCollectionRecreateListener(), (ChildWithBidirectionalManyToMany) newChild, index++);
    }
    checkNumberOfResults(listeners, index);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Transaction(org.hibernate.Transaction) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) Session(org.hibernate.Session) Test(org.junit.Test)

Example 19 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testUpdateParentOneChildToNoneByClear.

@Test
public void testUpdateParentOneChildToNoneByClear() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    assertEquals(1, parent.getChildren().size());
    Child child = (Child) parent.getChildren().iterator().next();
    listeners.clear();
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    if (child instanceof Entity) {
        child = (Child) s.get(child.getClass(), ((Entity) child).getId());
    }
    parent.clearChildren();
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) parent.getChildren()).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, index++);
    }
    if (child instanceof ChildWithBidirectionalManyToMany) {
        ChildWithBidirectionalManyToMany childWithManyToMany = (ChildWithBidirectionalManyToMany) child;
        if (((PersistentCollection) childWithManyToMany.getParents()).wasInitialized()) {
            checkResult(listeners, listeners.getInitializeCollectionListener(), childWithManyToMany, index++);
        }
    }
    checkResult(listeners, listeners.getPreCollectionUpdateListener(), parent, index++);
    checkResult(listeners, listeners.getPostCollectionUpdateListener(), parent, index++);
    if (child instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) child, index++);
        checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) child, index++);
    }
    checkNumberOfResults(listeners, index);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Transaction(org.hibernate.Transaction) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) Session(org.hibernate.Session) Test(org.junit.Test)

Example 20 with PersistentCollection

use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.

the class AbstractCollectionEventTest method testUpdateParentOneToTwoChildren.

@Test
public void testUpdateParentOneToTwoChildren() {
    CollectionListeners listeners = new CollectionListeners(sessionFactory());
    ParentWithCollection parent = createParentWithOneChild("parent", "child");
    assertEquals(1, parent.getChildren().size());
    listeners.clear();
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
    Child newChild = parent.addChild("new2");
    tx.commit();
    s.close();
    int index = 0;
    if (((PersistentCollection) parent.getChildren()).wasInitialized()) {
        checkResult(listeners, listeners.getInitializeCollectionListener(), parent, index++);
    }
    checkResult(listeners, listeners.getPreCollectionUpdateListener(), parent, index++);
    checkResult(listeners, listeners.getPostCollectionUpdateListener(), parent, index++);
    if (newChild instanceof ChildWithBidirectionalManyToMany) {
        checkResult(listeners, listeners.getPreCollectionRecreateListener(), (ChildWithBidirectionalManyToMany) newChild, index++);
        checkResult(listeners, listeners.getPostCollectionRecreateListener(), (ChildWithBidirectionalManyToMany) newChild, index++);
    }
    checkNumberOfResults(listeners, index);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Transaction(org.hibernate.Transaction) ChildWithBidirectionalManyToMany(org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

PersistentCollection (org.hibernate.collection.spi.PersistentCollection)53 Session (org.hibernate.Session)20 Test (org.junit.Test)20 Transaction (org.hibernate.Transaction)16 ChildWithBidirectionalManyToMany (org.hibernate.test.event.collection.association.bidirectional.manytomany.ChildWithBidirectionalManyToMany)15 Collection (java.util.Collection)13 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)11 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)8 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)7 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 Serializable (java.io.Serializable)6 CollectionKey (org.hibernate.engine.spi.CollectionKey)6 HibernateException (org.hibernate.HibernateException)5 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)5 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)5 Map (java.util.Map)4 IdentityMap (org.hibernate.internal.util.collections.IdentityMap)4 TestForIssue (org.hibernate.testing.TestForIssue)4 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3