Package org.hibernate.event

Examples of org.hibernate.event.EventSource


  }
   
  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 refresh event to be handled.
   */
  public void onRefresh(RefreshEvent event, Map refreshedAlready) {

    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.getCacheAccessStrategy().evict( 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

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

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

    final Serializable collectionKey = extractCollectionKeyFromOwner( persister );
    if ( collection!=null && (collection instanceof PersistentCollection) ) {
      PersistentCollection wrapper = (PersistentCollection) collection;
      if ( wrapper.setCurrentSession(session) ) {
View Full Code Here

     * @param event The auto-flush event to be handled.
     * @throws HibernateException
     */
  public void onAutoFlush(AutoFlushEvent event) throws HibernateException {

    final EventSource source = event.getSession();
   
    if ( flushMightBeNeeded(source) ) {

      final int oldSize = source.getActionQueue().numberOfCollectionRemovals();

      flushEverythingToExecutions(event);
     
      if ( flushIsReallyNeeded(event, source) ) {

        log.trace("Need to execute flush");

        performExecutions(source);
        postFlush(source);
        // note: performExecutions() clears all collectionXxxxtion
        // collections (the collection actions) in the session

        if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
          source.getFactory().getStatisticsImplementor().flush();
        }
       
      }
      else {

        log.trace("Dont need to execute flush");
        source.getActionQueue().clearFromFlushNeededCheck( oldSize );
      }
     
      event.setFlushRequired( flushIsReallyNeeded( event, source ) );

    }
View Full Code Here

          "reassociating transient instance: " +
              MessageHelper.infoString( persister, id, event.getSession().getFactory() )
      );
    }

    EventSource source = event.getSession();
    EntityKey key = new EntityKey( id, persister, source.getEntityMode() );

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

    //get a snapshot
    Object[] values = persister.getPropertyValues( object, source.getEntityMode() );
    TypeFactory.deepCopy(
        values,
        persister.getPropertyTypes(),
        persister.getPropertyUpdateability(),
        values,
        source
    );
    Object version = Versioning.getVersion( values, persister );

    EntityEntry newEntry = source.getPersistenceContext().addEntity(
        object,
        Status.MANAGED,
        values,
        key,
        version,
View Full Code Here

   * @param event The merge event to be handled.
   * @throws HibernateException
   */
  public void onMerge(MergeEvent event, Map copyCache) throws HibernateException {

    final EventSource source = event.getSession();
    final Object original = event.getOriginal();

    if ( original != null ) {

      final Object entity;
      if ( original instanceof HibernateProxy ) {
        LazyInitializer li = ( (HibernateProxy) original ).getHibernateLazyInitializer();
        if ( li.isUninitialized() ) {
          log.trace("ignoring uninitialized proxy");
          event.setResult( source.load( li.getEntityName(), li.getIdentifier() ) );
          return; //EARLY EXIT!
        }
        else {
          entity = li.getImplementation();
        }
      }
      else {
        entity = original;
      }
     
      if ( copyCache.containsKey(entity) ) {
        log.trace("already merged");
        event.setResult(entity);
      }
      else {

        event.setEntity( entity );

        int entityState = -1;

        // Check the persistence context for an entry relating to this
        // entity to be merged...
        EntityEntry entry = source.getPersistenceContext().getEntry( entity );
        if ( entry == null ) {
          EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
          Serializable id = persister.getIdentifier( entity, source.getEntityMode() );
          if ( id != null ) {
            EntityKey key = new EntityKey( id, persister, source.getEntityMode() );
            Object managedEntity = source.getPersistenceContext().getEntity( key );
            entry = source.getPersistenceContext().getEntry( managedEntity );
            if ( entry != null ) {
              // we have specialized case of a detached entity from the
              // perspective of the merge operation.  Specifically, we
              // have an incoming entity instance which has a corresponding
              // entry in the current persistence context, but registered
View Full Code Here

    log.trace("ignoring persistent instance");
   
    //TODO: check that entry.getIdentifier().equals(requestedId)
   
    final Object entity = event.getEntity();
    final EventSource source = event.getSession();
    final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
   
    copyCache.put(entity, entity); //before cascade!
   
    cascadeOnMerge(source, persister, entity, copyCache);
    copyValues(persister, entity, entity, source, copyCache);
View Full Code Here

  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;

    final Object copy = persister.instantiate( id, source.getEntityMode() )//TODO: should this be Session.instantiate(Persister, ...)?
    copyCache.put(entity, copy); //before cascade!
   
    // cascade first, so that all unsaved objects get their
    // copy created before we actually copy
    //cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
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

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.