use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.
the class ParentChildTest method testReplicate.
@Test
public void testReplicate() throws Exception {
Session s = openSession();
s.beginTransaction();
Container baz = new Container();
Contained f = new Contained();
List list = new ArrayList();
list.add(baz);
f.setBag(list);
List list2 = new ArrayList();
list2.add(f);
baz.setBag(list2);
s.save(f);
s.save(baz);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.replicate(baz, ReplicationMode.OVERWRITE);
// HHH-2378
SessionImpl x = (SessionImpl) s;
EntityEntry entry = x.getPersistenceContext().getEntry(baz);
assertNull(entry.getVersion());
// ~~~~~~~
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.replicate(baz, ReplicationMode.IGNORE);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.delete(baz);
s.delete(f);
s.getTransaction().commit();
s.close();
}
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 DefaultPersistEventListener method onPersist.
/**
* Handle the given create event.
*
* @param event The create event to be handled.
*
* @throws HibernateException
*/
public void onPersist(PersistEvent event, Map createCache) throws HibernateException {
final SessionImplementor source = event.getSession();
final Object object = event.getObject();
final Object entity;
if (object instanceof HibernateProxy) {
LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
if (li.isUninitialized()) {
if (li.getSession() == source) {
//NOTE EARLY EXIT!
return;
} else {
throw new PersistentObjectException("uninitialized proxy passed to persist()");
}
}
entity = li.getImplementation();
} else {
entity = object;
}
final String entityName;
if (event.getEntityName() != null) {
entityName = event.getEntityName();
} else {
entityName = source.bestGuessEntityName(entity);
event.setEntityName(entityName);
}
final EntityEntry entityEntry = source.getPersistenceContext().getEntry(entity);
EntityState entityState = getEntityState(entity, entityName, entityEntry, source);
if (entityState == EntityState.DETACHED) {
// JPA 2, in its version of a "foreign generated", allows the id attribute value
// to be manually set by the user, even though this manual value is irrelevant.
// The issue is that this causes problems with the Hibernate unsaved-value strategy
// which comes into play here in determining detached/transient state.
//
// Detect if we have this situation and if so null out the id value and calculate the
// entity state again.
// NOTE: entityEntry must be null to get here, so we cannot use any of its values
EntityPersister persister = source.getFactory().getEntityPersister(entityName);
if (ForeignGenerator.class.isInstance(persister.getIdentifierGenerator())) {
if (LOG.isDebugEnabled() && persister.getIdentifier(entity, source) != null) {
LOG.debug("Resetting entity id attribute to null for foreign generator");
}
persister.setIdentifier(entity, null, source);
entityState = getEntityState(entity, entityName, entityEntry, source);
}
}
switch(entityState) {
case DETACHED:
{
throw new PersistentObjectException("detached entity passed to persist: " + getLoggableName(event.getEntityName(), entity));
}
case PERSISTENT:
{
entityIsPersistent(event, createCache);
break;
}
case TRANSIENT:
{
entityIsTransient(event, createCache);
break;
}
case DELETED:
{
entityEntry.setStatus(Status.MANAGED);
entityEntry.setDeletedState(null);
event.getSession().getActionQueue().unScheduleDeletion(entityEntry, event.getObject());
entityIsDeleted(event, createCache);
break;
}
default:
{
throw new ObjectDeletedException("deleted entity passed to persist", null, getLoggableName(event.getEntityName(), entity));
}
}
}
use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.
the class DefaultFlushEntityEventListener method handleInterception.
protected boolean handleInterception(FlushEntityEvent event) {
SessionImplementor session = event.getSession();
EntityEntry entry = event.getEntityEntry();
EntityPersister persister = entry.getPersister();
Object entity = event.getEntity();
//give the Interceptor a chance to modify property values
final Object[] values = event.getPropertyValues();
final boolean intercepted = invokeInterceptor(session, entity, entry, values, persister);
//now we might need to recalculate the dirtyProperties array
if (intercepted && event.isDirtyCheckPossible() && !event.isDirtyCheckHandledByInterceptor()) {
int[] dirtyProperties;
if (event.hasDatabaseSnapshot()) {
dirtyProperties = persister.findModified(event.getDatabaseSnapshot(), values, entity, session);
} else {
dirtyProperties = persister.findDirty(values, entry.getLoadedState(), entity, session);
}
event.setDirtyProperties(dirtyProperties);
}
return intercepted;
}
use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.
the class AbstractFlushingEventListener method prepareEntityFlushes.
/**
* process cascade save/update at the start of a flush to discover
* any newly referenced entity that must be passed to saveOrUpdate(),
* and also apply orphan delete
*/
private void prepareEntityFlushes(EventSource session, PersistenceContext persistenceContext) throws HibernateException {
LOG.debug("Processing flush-time cascades");
final Object anything = getAnything();
//safe from concurrent modification because of how concurrentEntries() is implemented on IdentityMap
for (Map.Entry<Object, EntityEntry> me : persistenceContext.reentrantSafeEntityEntries()) {
// for ( Map.Entry me : IdentityMap.concurrentEntries( persistenceContext.getEntityEntries() ) ) {
EntityEntry entry = (EntityEntry) me.getValue();
Status status = entry.getStatus();
if (status == Status.MANAGED || status == Status.SAVING || status == Status.READ_ONLY) {
cascadeOnFlush(session, entry.getPersister(), me.getKey(), anything);
}
}
}
Aggregations