Search in sources :

Example 11 with AbstractPersistentCollection

use of org.hibernate.collection.internal.AbstractPersistentCollection in project hibernate-orm by hibernate.

the class VersionedCascadeDereferencedCollectionTest method testGetAndNullifyCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndNullifyCollection() {
    Session s = openSession();
    s.getTransaction().begin();
    VersionedCascadeOne one = new VersionedCascadeOne();
    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 = VersionedCascadeOne.class.getName() + ".manies";
    s = openSession();
    s.getTransaction().begin();
    one = (VersionedCascadeOne) s.get(VersionedCascadeOne.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 ceManies = getCollectionEntry(s, maniesEEOneStateOrig);
    assertNotNull(ceManies);
    assertEquals(role, ceManies.getRole());
    assertSame(sessionFactory().getCollectionPersister(role), ceManies.getLoadedPersister());
    assertEquals(one.getId(), ceManies.getKey());
    // nullify collection
    one.setManies(null);
    s.flush();
    // Ensure the same EntityEntry is being used.
    assertSame(eeOne, getEntityEntry(s, one));
    // Ensure one.getManies() is still null.
    assertNull(one.getManies());
    // 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(ceManies.getRole());
    assertNull(ceManies.getLoadedPersister());
    assertNull(ceManies.getKey());
    // Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
    // has key and role set to null.
    assertNull(maniesEEOneStateOrig.getKey());
    assertNull(maniesEEOneStateOrig.getRole());
    // Ensure eeOne.getLoadedState() returns null for collection after flush.
    assertNull(eeOne.getLoadedValue("manies"));
    // Ensure the session in maniesEEOneStateOrig has been unset.
    assertNull(maniesEEOneStateOrig.getSession());
    s.getTransaction().commit();
    s.close();
}
Also used : 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 12 with AbstractPersistentCollection

use of org.hibernate.collection.internal.AbstractPersistentCollection in project hibernate-orm by hibernate.

the class VersionedNoCascadeDereferencedCollectionTest method testMergeNullCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testMergeNullCollection() {
    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.merge(one);
    // after merging, one.getManies() should still be null;
    // the EntityEntry loaded state should contain a PersistentCollection though.
    assertNull(one.getManies());
    eeOne = getEntityEntry(s, one);
    AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue("manies");
    assertNotNull(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());
    s.flush();
    // Ensure the same EntityEntry is being used.
    assertSame(eeOne, getEntityEntry(s, one));
    // Ensure one.getManies() is still null.
    assertNull(one.getManies());
    // 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());
    // Ensure eeOne.getLoadedState() returns null for collection after flush.
    assertNull(eeOne.getLoadedValue("manies"));
    // Ensure the session in maniesEEOneStateOrig has been unset.
    assertNull(maniesEEOneStateOrig.getSession());
    s.getTransaction().commit();
    s.close();
}
Also used : 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 13 with AbstractPersistentCollection

use of org.hibernate.collection.internal.AbstractPersistentCollection in project hibernate-orm by hibernate.

the class UnversionedCascadeDereferencedCollectionTest method testGetAndNullifyCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndNullifyCollection() {
    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 ceManies = getCollectionEntry(s, maniesEEOneStateOrig);
    assertNotNull(ceManies);
    assertEquals(role, ceManies.getRole());
    assertSame(sessionFactory().getCollectionPersister(role), ceManies.getLoadedPersister());
    assertEquals(one.getId(), ceManies.getKey());
    // nullify collection
    one.setManies(null);
    s.flush();
    // Ensure the same EntityEntry is being used.
    assertSame(eeOne, getEntityEntry(s, one));
    // Ensure one.getManies() is still null.
    assertNull(one.getManies());
    // 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(ceManies.getRole());
    assertNull(ceManies.getLoadedPersister());
    assertNull(ceManies.getKey());
    // Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
    // has key and role set to null.
    assertNull(maniesEEOneStateOrig.getKey());
    assertNull(maniesEEOneStateOrig.getRole());
    // Ensure eeOne.getLoadedState() returns null for collection after flush.
    assertNull(eeOne.getLoadedValue("manies"));
    // Ensure the session in maniesEEOneStateOrig has been unset.
    assertNull(maniesEEOneStateOrig.getSession());
    s.getTransaction().commit();
    s.close();
}
Also used : 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)

Aggregations

AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)13 Session (org.hibernate.Session)12 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)12 EntityEntry (org.hibernate.engine.spi.EntityEntry)12 TestForIssue (org.hibernate.testing.TestForIssue)12 Test (org.junit.Test)12 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)5 MarkerObject (org.hibernate.internal.util.MarkerObject)1