Java Code Examples for org.hibernate.event.EventSource#getPersistenceContext()
The following examples show how to use
org.hibernate.event.EventSource#getPersistenceContext() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultDeleteEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
/** * Handle the given delete event. This is the cascaded form. * * @param event The delete event. * @param transientEntities The cache of entities already deleted * * @throws HibernateException */ public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException { final EventSource source = event.getSession(); final PersistenceContext persistenceContext = source.getPersistenceContext(); Object entity = persistenceContext.unproxyAndReassociate( event.getObject() ); EntityEntry entityEntry = persistenceContext.getEntry( entity ); final EntityPersister persister; final Serializable id; final Object version; if ( entityEntry == null ) { log.trace( "entity was not persistent in delete processing" ); persister = source.getEntityPersister( event.getEntityName(), entity ); if ( ForeignKeys.isTransient( persister.getEntityName(), entity, null, source ) ) { deleteTransientEntity( source, entity, event.isCascadeDeleteEnabled(), persister, transientEntities ); // EARLY EXIT!!! return; } else { performDetachedEntityDeletionCheck( event ); } id = persister.getIdentifier( entity, source.getEntityMode() ); if ( id == null ) { throw new TransientObjectException( "the detached instance passed to delete() had a null identifier" ); } EntityKey key = new EntityKey( id, persister, source.getEntityMode() ); persistenceContext.checkUniqueness( key, entity ); new OnUpdateVisitor( source, id, entity ).process( entity, persister ); version = persister.getVersion( entity, source.getEntityMode() ); entityEntry = persistenceContext.addEntity( entity, Status.MANAGED, persister.getPropertyValues( entity, source.getEntityMode() ), key, version, LockMode.NONE, true, persister, false, false ); } else { log.trace( "deleting a persistent instance" ); if ( entityEntry.getStatus() == Status.DELETED || entityEntry.getStatus() == Status.GONE ) { log.trace( "object was already deleted" ); return; } persister = entityEntry.getPersister(); id = entityEntry.getId(); version = entityEntry.getVersion(); } /*if ( !persister.isMutable() ) { throw new HibernateException( "attempted to delete an object of immutable class: " + MessageHelper.infoString(persister) ); }*/ if ( invokeDeleteLifecycle( source, entity, persister ) ) { return; } deleteEntity( source, entity, entityEntry, event.isCascadeDeleteEnabled(), persister, transientEntities ); if ( source.getFactory().getSettings().isIdentifierRollbackEnabled() ) { persister.resetIdentifier( entity, id, version, source.getEntityMode() ); } }
Example 2
Source File: DefaultDeleteEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
/** * 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.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 EntityPersister persister, final Set transientEntities) { if ( log.isTraceEnabled() ) { log.trace( "deleting " + 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, session.getEntityMode() ); } else { currentState = entityEntry.getLoadedState(); } final Object[] deletedState = createDeletedState( persister, currentState, session ); entityEntry.setDeletedState( deletedState ); session.getInterceptor().onDelete( entity, entityEntry.getId(), deletedState, persister.getPropertyNames(), propTypes ); // before any callbacks, etc, so subdeletions see that this deletion happened first persistenceContext.setEntryStatus( entityEntry, Status.DELETED ); EntityKey key = new EntityKey( entityEntry.getId(), persister, session.getEntityMode() ); 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 ); // Ensures that containing deletions happen before 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 after the flush, and will no longer // override the stale snapshot // This is now handled by removeEntity() in EntityDeleteAction //persistenceContext.removeDatabaseSnapshot(key); }
Example 3
Source File: DefaultLoadEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
private Object assembleCacheEntry( final CacheEntry entry, final Serializable id, final EntityPersister persister, final LoadEvent event) throws HibernateException { final Object optionalObject = event.getInstanceToLoad(); final EventSource session = event.getSession(); final SessionFactoryImplementor factory = session.getFactory(); if ( log.isTraceEnabled() ) { log.trace( "assembling entity from second-level cache: " + MessageHelper.infoString( persister, id, factory ) ); } EntityPersister subclassPersister = factory.getEntityPersister( entry.getSubclass() ); Object result = optionalObject == null ? session.instantiate( subclassPersister, id ) : optionalObject; // make it circular-reference safe TwoPhaseLoad.addUninitializedCachedEntity( new EntityKey( id, subclassPersister, session.getEntityMode() ), result, subclassPersister, LockMode.NONE, entry.areLazyPropertiesUnfetched(), entry.getVersion(), session ); Type[] types = subclassPersister.getPropertyTypes(); Object[] values = entry.assemble( result, id, subclassPersister, session.getInterceptor(), session ); // intializes result by side-effect TypeFactory.deepCopy( values, types, subclassPersister.getPropertyUpdateability(), values, session ); Object version = Versioning.getVersion( values, subclassPersister ); if ( log.isTraceEnabled() ) log.trace( "Cached Version: " + version ); final PersistenceContext persistenceContext = session.getPersistenceContext(); persistenceContext.addEntry( result, Status.MANAGED, values, null, id, version, LockMode.NONE, true, subclassPersister, false, entry.areLazyPropertiesUnfetched() ); subclassPersister.afterInitialize( result, entry.areLazyPropertiesUnfetched(), session ); persistenceContext.initializeNonLazyCollections(); // upgrade the lock if necessary: //lock(result, lockMode); //PostLoad is needed for EJB3 //TODO: reuse the PostLoadEvent... PostLoadEvent postLoadEvent = new PostLoadEvent(session).setEntity(result) .setId(id).setPersister(persister); PostLoadEventListener[] listeners = session.getListeners().getPostLoadEventListeners(); for ( int i = 0; i < listeners.length; i++ ) { listeners[i].onPostLoad(postLoadEvent); } return result; }
Example 4
Source File: AbstractFlushingEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
/** * Coordinates the processing necessary to get things ready for executions * as db calls by preping the session caches and moving the appropriate * entities and collections to their respective execution queues. * * @param event The flush event. * @throws HibernateException Error flushing caches to execution queues. */ protected void flushEverythingToExecutions(FlushEvent event) throws HibernateException { log.trace("flushing session"); EventSource session = event.getSession(); final PersistenceContext persistenceContext = session.getPersistenceContext(); session.getInterceptor().preFlush( new LazyIterator( persistenceContext.getEntitiesByKey() ) ); prepareEntityFlushes(session); // we could move this inside if we wanted to // tolerate collection initializations during // collection dirty checking: prepareCollectionFlushes(session); // now, any collections that are initialized // inside this block do not get updated - they // are ignored until the next flush persistenceContext.setFlushing(true); try { flushEntities(event); flushCollections(session); } finally { persistenceContext.setFlushing(false); } //some statistics if ( log.isDebugEnabled() ) { log.debug( "Flushed: " + session.getActionQueue().numberOfInsertions() + " insertions, " + session.getActionQueue().numberOfUpdates() + " updates, " + session.getActionQueue().numberOfDeletions() + " deletions to " + persistenceContext.getEntityEntries().size() + " objects" ); log.debug( "Flushed: " + session.getActionQueue().numberOfCollectionCreations() + " (re)creations, " + session.getActionQueue().numberOfCollectionUpdates() + " updates, " + session.getActionQueue().numberOfCollectionRemovals() + " removals to " + persistenceContext.getCollectionEntries().size() + " collections" ); new Printer( session.getFactory() ).toString( persistenceContext.getEntitiesByKey().values().iterator(), session.getEntityMode() ); } }