Search in sources :

Example 1 with AbstractPersistentCollection

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

the class UnversionedCascadeDereferencedCollectionTest method testMergeNullCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testMergeNullCollection(SessionFactoryScope scope) {
    UnversionedCascadeOne unversionedCascadeOne = new UnversionedCascadeOne();
    scope.inTransaction(session -> {
        assertNull(unversionedCascadeOne.getManies());
        session.save(unversionedCascadeOne);
        assertNull(unversionedCascadeOne.getManies());
        EntityEntry eeOne = getEntityEntry(session, unversionedCascadeOne);
        assertNull(eeOne.getLoadedValue("manies"));
        session.flush();
        assertNull(unversionedCascadeOne.getManies());
        assertNull(eeOne.getLoadedValue("manies"));
    });
    final String role = UnversionedCascadeOne.class.getName() + ".manies";
    scope.inTransaction(session -> {
        UnversionedCascadeOne one = (UnversionedCascadeOne) session.merge(unversionedCascadeOne);
        // after merging, one.getManies() should still be null;
        // the EntityEntry loaded state should contain a PersistentCollection though.
        assertNull(one.getManies());
        EntityEntry eeOne = getEntityEntry(session, 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(session, maniesEEOneStateOrig.getSession());
        // Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
        CollectionEntry ceManiesOrig = getCollectionEntry(session, maniesEEOneStateOrig);
        assertNotNull(ceManiesOrig);
        assertEquals(role, ceManiesOrig.getRole());
        assertSame(scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor(role), ceManiesOrig.getLoadedPersister());
        assertEquals(one.getId(), ceManiesOrig.getKey());
        session.flush();
        // Ensure the same EntityEntry is being used.
        assertSame(eeOne, getEntityEntry(session, one));
        // Ensure one.getManies() is still null.
        assertNull(one.getManies());
        // Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
        assertNull(getCollectionEntry(session, 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());
    });
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) AbstractPersistentCollection(org.hibernate.collection.spi.AbstractPersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with AbstractPersistentCollection

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

the class UnversionedCascadeDereferencedCollectionTest method testGetAndReplaceCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndReplaceCollection(SessionFactoryScope scope) {
    UnversionedCascadeOne unversionedCascadeOne = new UnversionedCascadeOne();
    scope.inTransaction(session -> {
        assertNull(unversionedCascadeOne.getManies());
        session.save(unversionedCascadeOne);
        assertNull(unversionedCascadeOne.getManies());
        EntityEntry eeOne = getEntityEntry(session, unversionedCascadeOne);
        assertNull(eeOne.getLoadedValue("manies"));
        session.flush();
        assertNull(unversionedCascadeOne.getManies());
        assertNull(eeOne.getLoadedValue("manies"));
    });
    final String role = UnversionedCascadeOne.class.getName() + ".manies";
    scope.inTransaction(session -> {
        UnversionedCascadeOne one = session.get(UnversionedCascadeOne.class, unversionedCascadeOne.getId());
        // When returned by Session.get(), one.getManies() will return a PersistentCollection;
        // the EntityEntry loaded state should contain the same PersistentCollection.
        EntityEntry eeOne = getEntityEntry(session, 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(session, maniesEEOneStateOrig.getSession());
        // Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
        CollectionEntry ceManiesOrig = getCollectionEntry(session, maniesEEOneStateOrig);
        assertNotNull(ceManiesOrig);
        assertEquals(role, ceManiesOrig.getRole());
        assertSame(scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor(role), ceManiesOrig.getLoadedPersister());
        assertEquals(one.getId(), ceManiesOrig.getKey());
        // replace collection
        one.setManies(new HashSet<>());
        session.flush();
        // Ensure the same EntityEntry is being used.
        assertSame(eeOne, getEntityEntry(session, one));
        // Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
        assertNull(getCollectionEntry(session, 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(session, ((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(session, (PersistentCollection) one.getManies());
        assertNotNull(ceManiesAfterReplace);
        assertEquals(role, ceManiesAfterReplace.getRole());
        assertSame(scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor(role), ceManiesAfterReplace.getLoadedPersister());
        assertEquals(one.getId(), ceManiesAfterReplace.getKey());
        // Ensure the session in maniesEEOneStateOrig has been unset.
        assertNull(maniesEEOneStateOrig.getSession());
    });
}
Also used : AbstractPersistentCollection(org.hibernate.collection.spi.AbstractPersistentCollection) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EntityEntry(org.hibernate.engine.spi.EntityEntry) AbstractPersistentCollection(org.hibernate.collection.spi.AbstractPersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with AbstractPersistentCollection

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

the class UnversionedNoCascadeDereferencedCollectionTest method testGetAndNullifyCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndNullifyCollection(SessionFactoryScope scope) {
    UnversionedNoCascadeOne unversionedNoCascadeOne = new UnversionedNoCascadeOne();
    scope.inTransaction(session -> {
        assertNull(unversionedNoCascadeOne.getManies());
        session.save(unversionedNoCascadeOne);
        assertNull(unversionedNoCascadeOne.getManies());
        EntityEntry eeOne = getEntityEntry(session, unversionedNoCascadeOne);
        assertNull(eeOne.getLoadedValue("manies"));
        session.flush();
        assertNull(unversionedNoCascadeOne.getManies());
        assertNull(eeOne.getLoadedValue("manies"));
    });
    final String role = UnversionedNoCascadeOne.class.getName() + ".manies";
    scope.inTransaction(session -> {
        UnversionedNoCascadeOne one = session.get(UnversionedNoCascadeOne.class, unversionedNoCascadeOne.getId());
        // When returned by Session.get(), one.getManies() will return a PersistentCollection;
        // the EntityEntry loaded state should contain the same PersistentCollection.
        EntityEntry eeOne = getEntityEntry(session, 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(session, maniesEEOneStateOrig.getSession());
        // Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
        CollectionEntry ceManies = getCollectionEntry(session, maniesEEOneStateOrig);
        assertNotNull(ceManies);
        assertEquals(role, ceManies.getRole());
        assertSame(scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor(role), ceManies.getLoadedPersister());
        assertEquals(one.getId(), ceManies.getKey());
        // nullify collection
        one.setManies(null);
        session.flush();
        // Ensure the same EntityEntry is being used.
        assertSame(eeOne, getEntityEntry(session, one));
        // Ensure one.getManies() is still null.
        assertNull(one.getManies());
        // Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
        assertNull(getCollectionEntry(session, 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());
    });
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) AbstractPersistentCollection(org.hibernate.collection.spi.AbstractPersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with AbstractPersistentCollection

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

the class VersionedCascadeDereferencedCollectionTest method testMergeNullCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testMergeNullCollection(SessionFactoryScope scope) {
    VersionedCascadeOne versionedCascadeOne = new VersionedCascadeOne();
    scope.inTransaction(session -> {
        assertNull(versionedCascadeOne.getManies());
        session.save(versionedCascadeOne);
        assertNull(versionedCascadeOne.getManies());
        EntityEntry eeOne = getEntityEntry(session, versionedCascadeOne);
        assertNull(eeOne.getLoadedValue("manies"));
        session.flush();
        assertNull(versionedCascadeOne.getManies());
        assertNull(eeOne.getLoadedValue("manies"));
    });
    final String role = VersionedCascadeOne.class.getName() + ".manies";
    scope.inTransaction(session -> {
        VersionedCascadeOne one = (VersionedCascadeOne) session.merge(versionedCascadeOne);
        // after merging, one.getManies() should still be null;
        // the EntityEntry loaded state should contain a PersistentCollection though.
        assertNull(one.getManies());
        EntityEntry eeOne = getEntityEntry(session, 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(session, maniesEEOneStateOrig.getSession());
        // Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
        CollectionEntry ceManiesOrig = getCollectionEntry(session, maniesEEOneStateOrig);
        assertNotNull(ceManiesOrig);
        assertEquals(role, ceManiesOrig.getRole());
        assertSame(scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getCollectionDescriptor(role), ceManiesOrig.getLoadedPersister());
        assertEquals(one.getId(), ceManiesOrig.getKey());
        session.flush();
        // Ensure the same EntityEntry is being used.
        assertSame(eeOne, getEntityEntry(session, one));
        // Ensure one.getManies() is still null.
        assertNull(one.getManies());
        // Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
        assertNull(getCollectionEntry(session, 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());
    });
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) AbstractPersistentCollection(org.hibernate.collection.spi.AbstractPersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with AbstractPersistentCollection

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

the class QueuedOperationCollectionAction method execute.

@Override
public void execute() throws HibernateException {
    // this QueuedOperationCollectionAction has to be executed before any other
    // CollectionAction involving the same collection.
    getPersister().processQueuedOps(getCollection(), getKey(), getSession());
    // TODO: It would be nice if this could be done safely by CollectionPersister#processQueuedOps;
    // Can't change the SPI to do this though.
    ((AbstractPersistentCollection<?>) getCollection()).clearOperationQueue();
    // The other CollectionAction types call CollectionEntry#afterAction, which
    // clears the dirty flag. We don't want to call CollectionEntry#afterAction unless
    // there is no other CollectionAction that will be executed on the same collection.
    final CollectionEntry ce = getSession().getPersistenceContextInternal().getCollectionEntry(getCollection());
    if (!ce.isDoremove() && !ce.isDoupdate() && !ce.isDorecreate()) {
        ce.afterAction(getCollection());
    }
}
Also used : AbstractPersistentCollection(org.hibernate.collection.spi.AbstractPersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry)

Aggregations

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