use of org.hibernate.engine.spi.SessionImplementor 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.SessionImplementor in project hibernate-orm by hibernate.
the class DefaultLoadEventListener method loadFromSecondLevelCache.
/**
* Attempts to load the entity from the second-level cache.
*
* @param event The load event
* @param persister The persister for the entity being requested for load
*
* @return The entity from the second-level cache, or null.
*/
private Object loadFromSecondLevelCache(final LoadEvent event, final EntityPersister persister, final EntityKey entityKey) {
final SessionImplementor source = event.getSession();
final boolean useCache = persister.hasCache() && source.getCacheMode().isGetEnabled() && event.getLockMode().lessThan(LockMode.READ);
if (!useCache) {
// we can't use cache here
return null;
}
final Object ce = getFromSharedCache(event, persister, source);
if (ce == null) {
// nothing was found in cache
return null;
}
return processCachedEntry(event, persister, ce, source, entityKey);
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class DefaultLoadEventListener method loadFromSessionCache.
/**
* Attempts to locate the entity in the session-level cache.
* <p/>
* If allowed to return nulls, then if the entity happens to be found in
* the session cache, we check the entity type for proper handling
* of entity hierarchies.
* <p/>
* If checkDeleted was set to true, then if the entity is found in the
* session-level cache, it's current status within the session cache
* is checked to see if it has previously been scheduled for deletion.
*
* @param event The load event
* @param keyToLoad The EntityKey representing the entity to be loaded.
* @param options The load options.
*
* @return The entity from the session-level cache, or null.
*
* @throws HibernateException Generally indicates problems applying a lock-mode.
*/
protected Object loadFromSessionCache(final LoadEvent event, final EntityKey keyToLoad, final LoadEventListener.LoadType options) throws HibernateException {
SessionImplementor session = event.getSession();
Object old = session.getEntityUsingInterceptor(keyToLoad);
if (old != null) {
// this object was already loaded
EntityEntry oldEntry = session.getPersistenceContext().getEntry(old);
if (options.isCheckDeleted()) {
Status status = oldEntry.getStatus();
if (status == Status.DELETED || status == Status.GONE) {
return REMOVED_ENTITY_MARKER;
}
}
if (options.isAllowNulls()) {
final EntityPersister persister = event.getSession().getFactory().getEntityPersister(keyToLoad.getEntityName());
if (!persister.isInstance(old)) {
return INCONSISTENT_RTN_CLASS_MARKER;
}
}
upgradeLock(old, oldEntry, event.getLockOptions(), event.getSession());
}
return old;
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class DirtyCollectionSearchVisitor method processCollection.
Object processCollection(Object collection, CollectionType type) throws HibernateException {
if (collection != null) {
final SessionImplementor session = getSession();
final PersistentCollection persistentCollection;
if (type.isArrayType()) {
persistentCollection = session.getPersistenceContext().getCollectionHolder(collection);
// if no array holder we found an unwrappered array (this can't occur,
// because we now always call wrap() beforeQuery getting to here)
// return (ah==null) ? true : searchForDirtyCollections(ah, type);
} else {
// if not wrappered yet, its dirty (this can't occur, because
// we now always call wrap() beforeQuery getting to here)
// return ( ! (obj instanceof PersistentCollection) ) ?
//true : searchForDirtyCollections( (PersistentCollection) obj, type );
persistentCollection = (PersistentCollection) collection;
}
if (persistentCollection.isDirty()) {
//we need to check even if it was not initialized, because of delayed adds!
dirty = true;
//NOTE: EARLY EXIT!
return null;
}
}
return null;
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class BasicOperationsTest method testCreateAndDelete.
@Test
public void testCreateAndDelete() {
Date now = new Date();
Session s = openSession();
s.doWork(new ValidateSomeEntityColumns((SessionImplementor) s));
s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_ENTITY_TABLE_NAME, 0));
s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_OTHER_ENTITY_TABLE_NAME, 0));
s.beginTransaction();
SomeEntity someEntity = new SomeEntity(now);
SomeOtherEntity someOtherEntity = new SomeOtherEntity(1);
s.save(someEntity);
s.save(someOtherEntity);
s.getTransaction().commit();
s.close();
s = openSession();
s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_ENTITY_TABLE_NAME, 1));
s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_OTHER_ENTITY_TABLE_NAME, 1));
s.beginTransaction();
s.delete(someEntity);
s.delete(someOtherEntity);
s.getTransaction().commit();
s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_ENTITY_TABLE_NAME, 0));
s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_OTHER_ENTITY_TABLE_NAME, 0));
s.close();
}
Aggregations