Package org.hibernate.event

Examples of org.hibernate.event.EventSource


    upgradeLock( entity, entry, event.getLockMode(), source );
  }
 
  private void cascadeOnLock(LockEvent event, EntityPersister persister, Object entity) {
    EventSource source = event.getSession();
    source.getPersistenceContext().incrementCascadeLevel();
    try {
      new Cascade(CascadingAction.LOCK, Cascade.AFTER_LOCK, source)
          .cascade( persister, entity, event.getLockMode() );
    }
    finally {
      source.getPersistenceContext().decrementCascadeLevel();
    }
  }
View Full Code Here


  }
   
  protected void entityIsPersistent(PersistEvent event, Map createCache) {
    log.trace("ignoring persistent instance");
    final EventSource source = event.getSession();
   
    //TODO: check that entry.getIdentifier().equals(requestedId)
   
    final Object entity = source.getPersistenceContext().unproxy( event.getObject() );
    final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
   
    if ( createCache.put(entity, entity)==null ) {
      //TODO: merge into one method!
      cascadeBeforeSave(source, persister, entity, createCache);
      cascadeAfterSave(source, persister, entity, createCache);
View Full Code Here

   */
  protected void entityIsTransient(PersistEvent event, Map createCache) throws HibernateException {
   
    log.trace("saving transient instance");

    final EventSource source = event.getSession();
   
    final Object entity = source.getPersistenceContext().unproxy( event.getObject() );
   
    if ( createCache.put(entity, entity)==null ) {
      saveWithGeneratedId( entity, event.getEntityName(), createCache, source, false );
    }

View Full Code Here

   *
   * @param event The flush event to be handled.
   * @throws HibernateException
   */
  public void onFlush(FlushEvent event) throws HibernateException {
    final EventSource source = event.getSession();
    if ( source.getPersistenceContext().hasNonReadOnlyEntities() ) {
     
      flushEverythingToExecutions(event);
      performExecutions(source);
      postFlush(source);
   
      if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
        source.getFactory().getStatisticsImplementor().flush();
      }
     
    }
  }
View Full Code Here

   * @param event The refresh event to be handled.
   * @throws HibernateException
   */
  public void onRefresh(RefreshEvent event, Map refreshedAlready) throws HibernateException {

    final EventSource source = event.getSession();
   
    if ( source.getPersistenceContext().reassociateIfUninitializedProxy( event.getObject() ) ) return;

    final Object object = source.getPersistenceContext().unproxyAndReassociate( event.getObject() );

    if ( refreshedAlready.containsKey(object) ) {
      log.trace("already refreshed");
      return;
    }

    final EntityEntry e = source.getPersistenceContext().getEntry( object );
    final EntityPersister persister;
    final Serializable id;
   
    if ( e == null ) {
      persister = source.getEntityPersister(null, object); //refresh() does not pass an entityName
      id = persister.getIdentifier( object, event.getSession().getEntityMode() );
      if ( log.isTraceEnabled() ) {
        log.trace(
            "refreshing transient " +
            MessageHelper.infoString( persister, id, source.getFactory() )
          );
      }
      EntityKey key = new EntityKey( id, persister, source.getEntityMode() );
      if ( source.getPersistenceContext().getEntry(key) != null ) {
        throw new PersistentObjectException(
            "attempted to refresh transient instance when persistent instance was already associated with the Session: " +
            MessageHelper.infoString(persister, id, source.getFactory() )
          );
      }
    }
    else {
      if ( log.isTraceEnabled() ) {
        log.trace(
            "refreshing " +
            MessageHelper.infoString( e.getPersister(), e.getId(), source.getFactory()  )
          );
      }
      if ( !e.isExistsInDatabase() ) {
        throw new HibernateException( "this instance does not yet exist as a row in the database" );
      }

      persister = e.getPersister();
      id = e.getId();
    }

    // cascade the refresh prior to refreshing this entity
    refreshedAlready.put(object, object);
    new Cascade(CascadingAction.REFRESH, Cascade.BEFORE_REFRESH, source)
        .cascade( persister, object, refreshedAlready );

    if ( e != null ) {
      EntityKey key = new EntityKey( id, persister, source.getEntityMode() );
      source.getPersistenceContext().removeEntity(key);
      if ( persister.hasCollections() ) new EvictVisitor( source ).process(object, persister);
    }

    if ( persister.hasCache() ) {
      final CacheKey ck = new CacheKey(
          id,
          persister.getIdentifierType(),
          persister.getRootEntityName(),
          source.getEntityMode(),
          source.getFactory()
        );
      persister.getCache().remove(ck);
    }
   
    evictCachedCollections( persister, id, source.getFactory() );
   
    String previousFetchProfile = source.getFetchProfile();
    source.setFetchProfile("refresh");
    Object result = persister.load( id, object, event.getLockMode(), source );
    source.setFetchProfile(previousFetchProfile);
   
    UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() );

  }
View Full Code Here

   *
   * @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() );
    }
  }
View Full Code Here

      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;
View Full Code Here

   * an update action, if necessary
   */
  public void onFlushEntity(FlushEntityEvent event) throws HibernateException {
    final Object entity = event.getEntity();
    final EntityEntry entry = event.getEntityEntry();
    final EventSource session = event.getSession();
    final EntityPersister persister = entry.getPersister();
    final Status status = entry.getStatus();
    final EntityMode entityMode = session.getEntityMode();
    final Type[] types = persister.getPropertyTypes();

    final boolean mightBeDirty = entry.requiresDirtyCheck(entity);

    final Object[] values = getValues( entity, entry, entityMode, mightBeDirty, session );
View Full Code Here

  }

  private boolean scheduleUpdate(final FlushEntityEvent event) {
   
    final EntityEntry entry = event.getEntityEntry();
    final EventSource session = event.getSession();
    final Object entity = event.getEntity();
    final Status status = entry.getStatus();
    final EntityMode entityMode = session.getEntityMode();
    final EntityPersister persister = entry.getPersister();
    final Object[] values = event.getPropertyValues();
   
    if ( log.isTraceEnabled() ) {
      if ( status == Status.DELETED ) {
        log.trace(
            "Updating deleted entity: " +
            MessageHelper.infoString( persister, entry.getId(), session.getFactory() )
          );
      }
      else {
        log.trace(
            "Updating entity: " +
            MessageHelper.infoString( persister, entry.getId(), session.getFactory()  )
          );
      }
    }

    boolean intercepted;
   
    if ( !entry.isBeingReplicated() ) {
      // give the Interceptor a chance to process property values, if the properties
      // were modified by the Interceptor, we need to set them back to the object
      intercepted = handleInterception( event );
    }
    else {
      intercepted = false;
    }

    validate( entity, persister, status, entityMode );

    // increment the version number (if necessary)
    final Object nextVersion = getNextVersion(event);

    // if it was dirtied by a collection only
    int[] dirtyProperties = event.getDirtyProperties();
    if ( event.isDirtyCheckPossible() && dirtyProperties == null ) {
      if ( ! intercepted && !event.hasDirtyCollection() ) {
        throw new AssertionFailure( "dirty, but no dirty properties" );
      }
      dirtyProperties = ArrayHelper.EMPTY_INT_ARRAY;
    }

    // check nullability but do not perform command execute
    // we'll use scheduled updates for that.
    new Nullability(session).checkNullability( values, persister, true );

    // schedule the update
    // note that we intentionally do _not_ pass in currentPersistentState!
    session.getActionQueue().addAction(
        new EntityUpdateAction(
            entry.getId(),
            values,
            dirtyProperties,
            event.hasDirtyCollection(),
View Full Code Here

    return super.invokeDeleteLifecycle( session, entity, persister );
  }

  @Override
  protected void performDetachedEntityDeletionCheck(DeleteEvent event) {
    EventSource source = event.getSession();
    String entityName = event.getEntityName();
    EntityPersister persister = source.getEntityPersister( entityName, event.getObject() );
    Serializable id =  persister.getIdentifier( event.getObject(), source.getEntityMode() );
    entityName = entityName == null ? source.guessEntityName( event.getObject() ) : entityName;
    throw new IllegalArgumentException("Removing a detached instance "+ entityName + "#" + id);
  }
View Full Code Here

TOP

Related Classes of org.hibernate.event.EventSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.