use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.
the class AbstractFlushingEventListener method postFlush.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Post-flushing section
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* 1. Recreate the collection key -> collection map
* 2. rebuild the collection entries
* 3. call Interceptor.postFlush()
*/
protected void postFlush(SessionImplementor session) throws HibernateException {
LOG.trace("Post flush");
final PersistenceContext persistenceContext = session.getPersistenceContext();
persistenceContext.getCollectionsByKey().clear();
// the database has changed now, so the subselect results need to be invalidated
// the batch fetching queues should also be cleared - especially the collection batch fetching one
persistenceContext.getBatchFetchQueue().clear();
for (Map.Entry<PersistentCollection, CollectionEntry> me : IdentityMap.concurrentEntries(persistenceContext.getCollectionEntries())) {
CollectionEntry collectionEntry = me.getValue();
PersistentCollection persistentCollection = me.getKey();
collectionEntry.postFlush(persistentCollection);
if (collectionEntry.getLoadedPersister() == null) {
//if the collection is dereferenced, unset its session reference and remove from the session cache
//iter.remove(); //does not work, since the entrySet is not backed by the set
persistentCollection.unsetSession(session);
persistenceContext.getCollectionEntries().remove(persistentCollection);
} else {
//otherwise recreate the mapping between the collection and its key
CollectionKey collectionKey = new CollectionKey(collectionEntry.getLoadedPersister(), collectionEntry.getLoadedKey());
persistenceContext.getCollectionsByKey().put(collectionKey, persistentCollection);
}
}
}
use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.
the class DefaultDeleteEventListener method deleteEntity.
/**
* Perform the entity deletion. Well, as with most operations, does not
* really perform it; just schedules an action/execution with the
* {@link org.hibernate.engine.spi.ActionQueue} for execution during flush.
*
* @param session The originating session
* @param entity The entity to delete
* @param entityEntry The entity's entry in the {@link PersistenceContext}
* @param isCascadeDeleteEnabled Is delete cascading enabled?
* @param persister The entity persister.
* @param transientEntities A cache of already deleted entities.
*/
protected final void deleteEntity(final EventSource session, final Object entity, final EntityEntry entityEntry, final boolean isCascadeDeleteEnabled, final boolean isOrphanRemovalBeforeUpdates, final EntityPersister persister, final Set transientEntities) {
if (LOG.isTraceEnabled()) {
LOG.tracev("Deleting {0}", MessageHelper.infoString(persister, entityEntry.getId(), session.getFactory()));
}
final PersistenceContext persistenceContext = session.getPersistenceContext();
final Type[] propTypes = persister.getPropertyTypes();
final Object version = entityEntry.getVersion();
final Object[] currentState;
if (entityEntry.getLoadedState() == null) {
//ie. the entity came in from update()
currentState = persister.getPropertyValues(entity);
} else {
currentState = entityEntry.getLoadedState();
}
final Object[] deletedState = createDeletedState(persister, currentState, session);
entityEntry.setDeletedState(deletedState);
session.getInterceptor().onDelete(entity, entityEntry.getId(), deletedState, persister.getPropertyNames(), propTypes);
// beforeQuery any callbacks, etc, so subdeletions see that this deletion happened first
persistenceContext.setEntryStatus(entityEntry, Status.DELETED);
final EntityKey key = session.generateEntityKey(entityEntry.getId(), persister);
cascadeBeforeDelete(session, persister, entity, entityEntry, transientEntities);
new ForeignKeys.Nullifier(entity, true, false, session).nullifyTransientReferences(entityEntry.getDeletedState(), propTypes);
new Nullability(session).checkNullability(entityEntry.getDeletedState(), persister, true);
persistenceContext.getNullifiableEntityKeys().add(key);
if (isOrphanRemovalBeforeUpdates) {
// TODO: The removeOrphan concept is a temporary "hack" for HHH-6484. This should be removed once action/task
// ordering is improved.
session.getActionQueue().addAction(new OrphanRemovalAction(entityEntry.getId(), deletedState, version, entity, persister, isCascadeDeleteEnabled, session));
} else {
// Ensures that containing deletions happen beforeQuery sub-deletions
session.getActionQueue().addAction(new EntityDeleteAction(entityEntry.getId(), deletedState, version, entity, persister, isCascadeDeleteEnabled, session));
}
cascadeAfterDelete(session, persister, entity, transientEntities);
// the entry will be removed afterQuery the flush, and will no longer
// override the stale snapshot
// This is now handled by removeEntity() in EntityDeleteAction
//persistenceContext.removeDatabaseSnapshot(key);
}
use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.
the class Loader method getResultFromQueryCache.
private List getResultFromQueryCache(final SharedSessionContractImplementor session, final QueryParameters queryParameters, final Set<Serializable> querySpaces, final Type[] resultTypes, final QueryCache queryCache, final QueryKey key) {
List result = null;
if (session.getCacheMode().isGetEnabled()) {
boolean isImmutableNaturalKeyLookup = queryParameters.isNaturalKeyLookup() && resultTypes.length == 1 && resultTypes[0].isEntityType() && getEntityPersister(EntityType.class.cast(resultTypes[0])).getEntityMetamodel().hasImmutableNaturalId();
final PersistenceContext persistenceContext = session.getPersistenceContext();
boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
if (queryParameters.isReadOnlyInitialized()) {
// The read-only/modifiable mode for the query was explicitly set.
// Temporarily set the default read-only/modifiable setting to the query's setting.
persistenceContext.setDefaultReadOnly(queryParameters.isReadOnly());
} else {
// The read-only/modifiable setting for the query was not initialized.
// Use the default read-only/modifiable from the persistence context instead.
queryParameters.setReadOnly(persistenceContext.isDefaultReadOnly());
}
try {
result = queryCache.get(key, key.getResultTransformer().getCachedResultTypes(resultTypes), isImmutableNaturalKeyLookup, querySpaces, session);
} finally {
persistenceContext.setDefaultReadOnly(defaultReadOnlyOrig);
}
if (factory.getStatistics().isStatisticsEnabled()) {
if (result == null) {
factory.getStatistics().queryCacheMiss(getQueryIdentifier(), queryCache.getRegion().getName());
} else {
factory.getStatistics().queryCacheHit(getQueryIdentifier(), queryCache.getRegion().getName());
}
}
}
return result;
}
use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.
the class Loader method doQueryAndInitializeNonLazyCollections.
public List doQueryAndInitializeNonLazyCollections(final SharedSessionContractImplementor session, final QueryParameters queryParameters, final boolean returnProxies, final ResultTransformer forcedResultTransformer) throws HibernateException, SQLException {
final PersistenceContext persistenceContext = session.getPersistenceContext();
boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
if (queryParameters.isReadOnlyInitialized()) {
// The read-only/modifiable mode for the query was explicitly set.
// Temporarily set the default read-only/modifiable setting to the query's setting.
persistenceContext.setDefaultReadOnly(queryParameters.isReadOnly());
} else {
// The read-only/modifiable setting for the query was not initialized.
// Use the default read-only/modifiable from the persistence context instead.
queryParameters.setReadOnly(persistenceContext.isDefaultReadOnly());
}
persistenceContext.beforeLoad();
List result;
try {
try {
result = doQuery(session, queryParameters, returnProxies, forcedResultTransformer);
} finally {
persistenceContext.afterLoad();
}
persistenceContext.initializeNonLazyCollections();
} finally {
// Restore the original default
persistenceContext.setDefaultReadOnly(defaultReadOnlyOrig);
}
return result;
}
use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.
the class AbstractLoadPlanBasedLoader method executeLoad.
protected List executeLoad(SharedSessionContractImplementor session, QueryParameters queryParameters, LoadQueryDetails loadQueryDetails, boolean returnProxies, ResultTransformer forcedResultTransformer, List<AfterLoadAction> afterLoadActions) throws SQLException {
final PersistenceContext persistenceContext = session.getPersistenceContext();
final boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
if (queryParameters.isReadOnlyInitialized()) {
// The read-only/modifiable mode for the query was explicitly set.
// Temporarily set the default read-only/modifiable setting to the query's setting.
persistenceContext.setDefaultReadOnly(queryParameters.isReadOnly());
} else {
// The read-only/modifiable setting for the query was not initialized.
// Use the default read-only/modifiable from the persistence context instead.
queryParameters.setReadOnly(persistenceContext.isDefaultReadOnly());
}
persistenceContext.beforeLoad();
try {
List results = null;
final String sql = loadQueryDetails.getSqlStatement();
SqlStatementWrapper wrapper = null;
try {
wrapper = executeQueryStatement(sql, queryParameters, false, afterLoadActions, session);
results = loadQueryDetails.getResultSetProcessor().extractResults(wrapper.getResultSet(), session, queryParameters, new NamedParameterContext() {
@Override
public int[] getNamedParameterLocations(String name) {
return AbstractLoadPlanBasedLoader.this.getNamedParameterLocs(name);
}
}, returnProxies, queryParameters.isReadOnly(), forcedResultTransformer, afterLoadActions);
} finally {
if (wrapper != null) {
session.getJdbcCoordinator().getResourceRegistry().release(wrapper.getResultSet(), wrapper.getStatement());
session.getJdbcCoordinator().getResourceRegistry().release(wrapper.getStatement());
session.getJdbcCoordinator().afterStatementExecution();
}
persistenceContext.afterLoad();
}
persistenceContext.initializeNonLazyCollections();
return results;
} finally {
// Restore the original default
persistenceContext.setDefaultReadOnly(defaultReadOnlyOrig);
}
}
Aggregations