Search in sources :

Example 1 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class AbstractEntityInsertAction method afterDeserialize.

@Override
public void afterDeserialize(SharedSessionContractImplementor session) {
    super.afterDeserialize(session);
    // guard against NullPointerException
    if (session != null) {
        final EntityEntry entityEntry = session.getPersistenceContext().getEntry(getInstance());
        this.state = entityEntry.getLoadedState();
    }
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry)

Example 2 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry 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 3 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class UnversionedNoCascadeDereferencedCollectionTest method testSaveOrUpdateNullCollection.

@Test
public void testSaveOrUpdateNullCollection() {
    Session s = openSession();
    s.getTransaction().begin();
    UnversionedNoCascadeOne one = new UnversionedNoCascadeOne();
    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();
    s = openSession();
    s.getTransaction().begin();
    s.saveOrUpdate(one);
    // Ensure one.getManies() is still null.
    assertNull(one.getManies());
    // Ensure the EntityEntry loaded state contains null for the manies collection.
    eeOne = getEntityEntry(s, one);
    assertNull(eeOne.getLoadedValue("manies"));
    s.flush();
    // Ensure one.getManies() is still null.
    assertNull(one.getManies());
    // Ensure the same EntityEntry is being used.
    assertSame(eeOne, getEntityEntry(s, one));
    // Ensure the EntityEntry loaded state still contains null for the manies collection.
    assertNull(eeOne.getLoadedValue("manies"));
    s.getTransaction().commit();
    s.close();
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry 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 5 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class UnversionedCascadeDereferencedCollectionTest method testSaveOrUpdateNullCollection.

@Test
public void testSaveOrUpdateNullCollection() {
    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();
    s = openSession();
    s.getTransaction().begin();
    s.saveOrUpdate(one);
    // Ensure one.getManies() is still null.
    assertNull(one.getManies());
    // Ensure the EntityEntry loaded state contains null for the manies collection.
    eeOne = getEntityEntry(s, one);
    assertNull(eeOne.getLoadedValue("manies"));
    s.flush();
    // Ensure one.getManies() is still null.
    assertNull(one.getManies());
    // Ensure the same EntityEntry is being used.
    assertSame(eeOne, getEntityEntry(s, one));
    // Ensure the EntityEntry loaded state still contains null for the manies collection.
    assertNull(eeOne.getLoadedValue("manies"));
    s.getTransaction().commit();
    s.close();
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

EntityEntry (org.hibernate.engine.spi.EntityEntry)82 Serializable (java.io.Serializable)23 EntityPersister (org.hibernate.persister.entity.EntityPersister)22 Session (org.hibernate.Session)18 Test (org.junit.Test)17 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)16 HibernateProxy (org.hibernate.proxy.HibernateProxy)13 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)12 EntityKey (org.hibernate.engine.spi.EntityKey)12 TestForIssue (org.hibernate.testing.TestForIssue)12 AssertionFailure (org.hibernate.AssertionFailure)11 HibernateException (org.hibernate.HibernateException)10 EventSource (org.hibernate.event.spi.EventSource)9 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)8 TransientObjectException (org.hibernate.TransientObjectException)7 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)7 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)7 Status (org.hibernate.engine.spi.Status)6 LazyInitializer (org.hibernate.proxy.LazyInitializer)6 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)5