Search in sources :

Example 81 with EntityEntry

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

the class VersionedNoCascadeDereferencedCollectionTest method testSaveOrUpdateNullCollection.

@Test
public void testSaveOrUpdateNullCollection() {
    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();
    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 82 with EntityEntry

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

the class OptimisticLockingStrategy method lock.

@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
    if (!lockable.isVersioned()) {
        throw new OptimisticLockException(object, "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]");
    }
    final EntityEntry entry = session.getPersistenceContext().getEntry(object);
    // Register the EntityVerifyVersionProcess action to run just prior to transaction commit.
    ((EventSource) session).getActionQueue().registerProcess(new EntityVerifyVersionProcess(object, entry));
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) EntityVerifyVersionProcess(org.hibernate.action.internal.EntityVerifyVersionProcess) OptimisticLockException(org.hibernate.OptimisticLockException)

Example 83 with EntityEntry

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

the class EvictAuditDataAfterCommitTest method checkEmptyAuditSessionCache.

private void checkEmptyAuditSessionCache(Session session, String... auditEntityNames) {
    List<String> entityNames = Arrays.asList(auditEntityNames);
    PersistenceContext persistenceContext = ((SessionImplementor) session).getPersistenceContext();
    for (Map.Entry<Object, EntityEntry> entrySet : persistenceContext.reentrantSafeEntityEntries()) {
        final EntityEntry entityEntry = entrySet.getValue();
        if (entityNames.contains(entityEntry.getEntityName())) {
            assert false : "Audit data shall not be stored in the session level cache. This causes performance issues.";
        }
        Assert.assertFalse("Revision entity shall not be stored in the session level cache. This causes performance issues.", SequenceIdRevisionEntity.class.getName().equals(entityEntry.getEntityName()));
    }
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Map(java.util.Map)

Example 84 with EntityEntry

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

the class Loader method initializeEntitiesAndCollections.

private void initializeEntitiesAndCollections(final List hydratedObjects, final Object resultSetId, final SharedSessionContractImplementor session, final boolean readOnly, List<AfterLoadAction> afterLoadActions) throws HibernateException {
    final CollectionPersister[] collectionPersisters = getCollectionPersisters();
    if (collectionPersisters != null) {
        for (CollectionPersister collectionPersister : collectionPersisters) {
            if (collectionPersister.isArray()) {
                // for arrays, we should end the collection load before resolving
                // the entities, since the actual array instances are not instantiated
                // during loading
                // TODO: or we could do this polymorphically, and have two
                // different operations implemented differently for arrays
                endCollectionLoad(resultSetId, session, collectionPersister);
            }
        }
    }
    // important: reuse the same event instances for performance!
    final PreLoadEvent pre;
    final PostLoadEvent post;
    if (session.isEventSource()) {
        pre = new PreLoadEvent((EventSource) session);
        post = new PostLoadEvent((EventSource) session);
    } else {
        pre = null;
        post = null;
    }
    if (hydratedObjects != null) {
        int hydratedObjectsSize = hydratedObjects.size();
        LOG.tracev("Total objects hydrated: {0}", hydratedObjectsSize);
        for (Object hydratedObject : hydratedObjects) {
            TwoPhaseLoad.initializeEntity(hydratedObject, readOnly, session, pre);
        }
    }
    if (collectionPersisters != null) {
        for (CollectionPersister collectionPersister : collectionPersisters) {
            if (!collectionPersister.isArray()) {
                // for sets, we should end the collection load after resolving
                // the entities, since we might call hashCode() on the elements
                // TODO: or we could do this polymorphically, and have two
                // different operations implemented differently for arrays
                endCollectionLoad(resultSetId, session, collectionPersister);
            }
        }
    }
    // persistence context.
    if (hydratedObjects != null) {
        for (Object hydratedObject : hydratedObjects) {
            TwoPhaseLoad.postLoad(hydratedObject, session, post);
            if (afterLoadActions != null) {
                for (AfterLoadAction afterLoadAction : afterLoadActions) {
                    final EntityEntry entityEntry = session.getPersistenceContext().getEntry(hydratedObject);
                    if (entityEntry == null) {
                        // big problem
                        throw new HibernateException("Could not locate EntityEntry immediately after two-phase load");
                    }
                    afterLoadAction.afterLoad(session, hydratedObject, (Loadable) entityEntry.getPersister());
                }
            }
        }
    }
}
Also used : PostLoadEvent(org.hibernate.event.spi.PostLoadEvent) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) HibernateException(org.hibernate.HibernateException) AfterLoadAction(org.hibernate.loader.spi.AfterLoadAction) PreLoadEvent(org.hibernate.event.spi.PreLoadEvent)

Example 85 with EntityEntry

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

the class UnversionedNoCascadeDereferencedCollectionTest method testGetAndNullifyCollection.

@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndNullifyCollection() {
    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();
    final String role = UnversionedNoCascadeOne.class.getName() + ".manies";
    s = openSession();
    s.getTransaction().begin();
    one = (UnversionedNoCascadeOne) s.get(UnversionedNoCascadeOne.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

EntityEntry (org.hibernate.engine.spi.EntityEntry)140 EntityPersister (org.hibernate.persister.entity.EntityPersister)34 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)28 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)27 TestForIssue (org.hibernate.testing.TestForIssue)26 Test (org.junit.Test)23 EntityKey (org.hibernate.engine.spi.EntityKey)22 Session (org.hibernate.Session)19 HibernateProxy (org.hibernate.proxy.HibernateProxy)18 Test (org.junit.jupiter.api.Test)18 EventSource (org.hibernate.event.spi.EventSource)16 HibernateException (org.hibernate.HibernateException)13 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)13 Serializable (java.io.Serializable)12 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)12 AbstractPersistentCollection (org.hibernate.collection.spi.AbstractPersistentCollection)12 AssertionFailure (org.hibernate.AssertionFailure)11 Type (org.hibernate.type.Type)11 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)10 Status (org.hibernate.engine.spi.Status)10