Package org.hibernate.event

Examples of org.hibernate.event.EventSource


  protected void entityIsTransient(MergeEvent event, Map copyCache) {
   
    log.trace("merging transient instance");
   
    final Object entity = event.getEntity();
    final EventSource source = event.getSession();

    final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
    final String entityName = persister.getEntityName();
   
    final Serializable id = persister.hasIdentifierProperty() ?
        persister.getIdentifier( entity, source.getEntityMode() ) :
            null;
    if ( copyCache.containsKey( entity ) ) {
      persister.setIdentifier( copyCache.get( entity ), id, source.getEntityMode() );
    }
    else {
      copyCache.put(entity, persister.instantiate( id, source.getEntityMode() ) ); //before cascade!
      //TODO: should this be Session.instantiate(Persister, ...)?
    }
    final Object copy = copyCache.get( entity );

    // cascade first, so that all unsaved objects get their
View Full Code Here


  protected void entityIsDetached(MergeEvent event, Map copyCache) {
   
    log.trace("merging detached instance");
   
    final Object entity = event.getEntity();
    final EventSource source = event.getSession();

    final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
    final String entityName = persister.getEntityName();
     
    Serializable id = event.getRequestedId();
    if ( id == null ) {
      id = persister.getIdentifier( entity, source.getEntityMode() );
    }
    else {
      // check that entity id = requestedId
      Serializable entityId = persister.getIdentifier( entity, source.getEntityMode() );
      if ( !persister.getIdentifierType().isEqual( id, entityId, source.getEntityMode(), source.getFactory() ) ) {
        throw new HibernateException( "merge requested with id not matching id of passed entity" );
      }
    }
   
    String previousFetchProfile = source.getFetchProfile();
    source.setFetchProfile("merge");
    //we must clone embedded composite identifiers, or
    //we will get back the same instance that we pass in
    final Serializable clonedIdentifier = (Serializable) persister.getIdentifierType()
        .deepCopy( id, source.getEntityMode(), source.getFactory() );
    final Object result = source.get(entityName, clonedIdentifier);
    source.setFetchProfile(previousFetchProfile);
   
    if ( result == null ) {
      //TODO: we should throw an exception if we really *know* for sure 
      //      that this is a detached instance, rather than just assuming
      //throw new StaleObjectStateException(entityName, id);
     
      // we got here because we assumed that an instance
      // with an assigned id was detached, when it was
      // really persistent
      entityIsTransient(event, copyCache);
    }
    else {
      copyCache.put(entity, result); //before cascade!
 
      final Object target = source.getPersistenceContext().unproxy(result);
      if ( target == entity ) {
        throw new AssertionFailure("entity was not detached");
      }
      else if ( !source.getEntityName(target).equals(entityName) ) {
        throw new WrongClassException(
            "class of the given object did not match class of persistent copy",
            event.getRequestedId(),
            entityName
          );
      }
      else if ( isVersionChanged( entity, source, persister, target ) ) {
        if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
          source.getFactory().getStatisticsImplementor()
              .optimisticFailure( entityName );
        }
        throw new StaleObjectStateException( entityName, id );
      }
 
View Full Code Here

    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

    if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
      return null;
    }

    EventSource session = getSession();
    CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );

    if ( isUpdate ) {
      removeCollection( persister, extractCollectionKeyFromOwner( persister ), session );
    }
    if ( collection != null && ( collection instanceof PersistentCollection ) ) {
      PersistentCollection wrapper = ( PersistentCollection ) collection;
      wrapper.setCurrentSession( session );
      if ( wrapper.wasInitialized() ) {
        session.getPersistenceContext().addNewCollection( persister, wrapper );
      }
      else {
        reattachCollection( wrapper, type );
      }
    }
View Full Code Here

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

    // be loaded.

    // So this needs to be safe from concurrent modification problems.
    // It is safe because of how IdentityMap implements entrySet()

    final EventSource source = event.getSession();
   
    final Map.Entry[] list = IdentityMap.concurrentEntries( source.getPersistenceContext().getEntityEntries() );
    final int size = list.length;
    for ( int i = 0; i < size; i++ ) {

      // Update the status of the object and if necessary, schedule an update

      Map.Entry me = list[i];
      EntityEntry entry = (EntityEntry) me.getValue();
      Status status = entry.getStatus();

      if ( status != Status.LOADING && status != Status.GONE ) {
        FlushEntityEvent entityEvent = new FlushEntityEvent( source, me.getKey(), entry );
        FlushEntityEventListener[] listeners = source.getListeners().getFlushEntityEventListeners();
        for ( int j = 0; j < listeners.length; j++ ) {
          listeners[j].onFlushEntity(entityEvent);
        }
      }
    }

    source.getActionQueue().sortActions();
  }
View Full Code Here

   *
   * @param event The evict event to be handled.
   * @throws HibernateException
   */
  public void onEvict(EvictEvent event) throws HibernateException {
    EventSource source = event.getSession();
    final Object object = event.getObject();
    final PersistenceContext persistenceContext = source.getPersistenceContext();

    if ( object instanceof HibernateProxy ) {
      LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
      Serializable id = li.getIdentifier();
      EntityPersister persister = source.getFactory().getEntityPersister( li.getEntityName() );
      if ( id == null ) {
        throw new IllegalArgumentException("null identifier");
      }

      EntityKey key = new EntityKey( id, persister, source.getEntityMode() );
      persistenceContext.removeProxy( key );

      if ( !li.isUninitialized() ) {
        final Object entity = persistenceContext.removeEntity( key );
        if ( entity != null ) {
          EntityEntry e = event.getSession().getPersistenceContext().removeEntry( entity );
          doEvict( entity, key, e.getPersister(), event.getSession() );
        }
      }
      li.setSession( null );
    }
    else {
      EntityEntry e = persistenceContext.removeEntry( object );
      if ( e != null ) {
        EntityKey key = new EntityKey( e.getId(), e.getPersister(), source.getEntityMode()  );
        persistenceContext.removeEntity( key );
        doEvict( object, key, e.getPersister(), source );
      }
    }
  }
View Full Code Here

   */
  protected Serializable entityIsTransient(SaveOrUpdateEvent event) {

    log.trace( "saving transient instance" );

    final EventSource source = event.getSession();

    EntityEntry entityEntry = event.getEntry();
    if ( entityEntry != null ) {
      if ( entityEntry.getStatus() == Status.DELETED ) {
        source.forceFlush( entityEntry );
      }
      else {
        throw new AssertionFailure( "entity was persistent" );
      }
    }

    Serializable id = saveWithGeneratedOrRequestedId( event );

    source.getPersistenceContext().reassociateProxy( event.getObject(), id );

    return id;
  }
View Full Code Here

                    persister, event.getRequestedId(), event.getSession().getFactory()
                )
        );
      }

      final EventSource source = event.getSession();

      EntityKey key = new EntityKey( event.getRequestedId(), persister, source.getEntityMode() );

      source.getPersistenceContext().checkUniqueness( key, entity );

      if ( invokeUpdateLifecycle( entity, persister, source ) ) {
        reassociate( event, event.getObject(), event.getRequestedId(), persister );
        return;
      }

      // this is a transient object with existing persistent state not loaded by the session

      new OnUpdateVisitor( source, event.getRequestedId(), entity ).process( entity, persister );

      //TODO: put this stuff back in to read snapshot from
      //      the second-level cache (needs some extra work)
      /*Object[] cachedState = null;

      if ( persister.hasCache() ) {
        CacheEntry entry = (CacheEntry) persister.getCache()
            .get( event.getRequestedId(), source.getTimestamp() );
          cachedState = entry==null ?
              null :
              entry.getState(); //TODO: half-assemble this stuff
      }*/

      source.getPersistenceContext().addEntity(
          entity,
          Status.MANAGED,
          null, //cachedState,
          key,
          persister.getVersion( entity, source.getEntityMode() ),
          LockMode.NONE,
          true,
          persister,
          false,
          true //assume true, since we don't really know, and it doesn't matter
      );

      persister.afterReassociate( entity, source );

      if ( log.isTraceEnabled() ) {
        log.trace(
            "updating " +
                MessageHelper.infoString( persister, event.getRequestedId(), source.getFactory() )
        );
      }

      cascadeOnUpdate( event, persister, entity );

View Full Code Here

   * @param event The event currently being processed.
   * @param persister The defined persister for the entity being updated.
   * @param entity The entity being updated.
   */
  private void cascadeOnUpdate(SaveOrUpdateEvent event, EntityPersister persister, Object entity) {
    EventSource source = event.getSession();
    source.getPersistenceContext().incrementCascadeLevel();
    try {
      new Cascade( CascadingAction.SAVE_UPDATE, Cascade.AFTER_UPDATE, source )
          .cascade( persister, entity );
    }
    finally {
      source.getPersistenceContext().decrementCascadeLevel();
    }
  }
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.