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();
}
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));
}
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()));
}
}
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());
}
}
}
}
}
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();
}
Aggregations