Package org.hibernate.engine

Examples of org.hibernate.engine.SessionImplementor


   * complete.
   *
   * @param persister The persister for which to complete loading.
   */
  public void endLoadingCollections(CollectionPersister persister) {
    SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
    if ( !loadContexts.hasLoadingCollectionEntries()
        && localLoadingCollectionKeys.isEmpty() ) {
      return;
    }

    // in an effort to avoid concurrent-modification-exceptions (from
    // potential recursive calls back through here as a result of the
    // eventual call to PersistentCollection#endRead), we scan the
    // internal loadingCollections map for matches and store those matches
    // in a temp collection.  the temp collection is then used to "drive"
    // the #endRead processing.
    List matches = null;
    Iterator iter = localLoadingCollectionKeys.iterator();
    while ( iter.hasNext() ) {
      final CollectionKey collectionKey = (CollectionKey) iter.next();
      final LoadingCollectionEntry lce = loadContexts.locateLoadingCollectionEntry( collectionKey );
      if ( lce == null) {
        log.warn( "In CollectionLoadContext#endLoadingCollections, localLoadingCollectionKeys contained [" + collectionKey + "], but no LoadingCollectionEntry was found in loadContexts" );
      }
      else if ( lce.getResultSet() == resultSet && lce.getPersister() == persister ) {
        if ( matches == null ) {
          matches = new ArrayList();
        }
        matches.add( lce );
        if ( lce.getCollection().getOwner() == null ) {
          session.getPersistenceContext().addUnownedCollection(
              new CollectionKey( persister, lce.getKey(), session.getEntityMode() ),
              lce.getCollection()
          );
        }
        if ( log.isTraceEnabled() ) {
          log.trace( "removing collection load entry [" + lce + "]" );
View Full Code Here


  private void endLoadingCollection(LoadingCollectionEntry lce, CollectionPersister persister) {
    if ( log.isTraceEnabled() ) {
      log.debug( "ending loading collection [" + lce + "]" );
    }
    final SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
    final EntityMode em = session.getEntityMode();

    boolean hasNoQueuedAdds = lce.getCollection().endRead(); // warning: can cause a recursive calls! (proxy initialization)

    if ( persister.getCollectionType().hasHolder( em ) ) {
      getLoadContext().getPersistenceContext().addCollectionHolder( lce.getCollection() );
    }

    CollectionEntry ce = getLoadContext().getPersistenceContext().getCollectionEntry( lce.getCollection() );
    if ( ce == null ) {
      ce = getLoadContext().getPersistenceContext().addInitializedCollection( persister, lce.getCollection(), lce.getKey() );
    }
    else {
      ce.postInitialize( lce.getCollection() );
    }

    boolean addToCache = hasNoQueuedAdds && // there were no queued additions
        persister.hasCache() &&             // and the role has a cache
        session.getCacheMode().isPutEnabled() &&
        !ce.isDoremove();                   // and this is not a forced initialization during flush
    if ( addToCache ) {
      addCollectionToCache( lce, persister );
    }

    if ( log.isDebugEnabled() ) {
      log.debug( "collection fully initialized: " + MessageHelper.collectionInfoString(persister, lce.getKey(), session.getFactory() ) );
    }

    if ( session.getFactory().getStatistics().isStatisticsEnabled() ) {
      session.getFactory().getStatisticsImplementor().loadCollection( persister.getRole() );
    }
  }
View Full Code Here

   *
   * @param lce The entry representing the collection to add
   * @param persister The persister
   */
  private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersister persister) {
    final SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
    final SessionFactoryImplementor factory = session.getFactory();

    if ( log.isDebugEnabled() ) {
      log.debug( "Caching collection: " + MessageHelper.collectionInfoString( persister, lce.getKey(), factory ) );
    }

    if ( !session.getEnabledFilters().isEmpty() && persister.isAffectedByEnabledFilters( session ) ) {
      // some filters affecting the collection are enabled on the session, so do not do the put into the cache.
      log.debug( "Refusing to add to cache due to enabled filters" );
      // todo : add the notion of enabled filters to the CacheKey to differentiate filtered collections from non-filtered;
      //      but CacheKey is currently used for both collections and entities; would ideally need to define two seperate ones;
      //      currently this works in conjuction with the check on
      //      DefaultInitializeCollectionEventHandler.initializeCollectionFromCache() (which makes sure to not read from
      //      cache with enabled filters).
      return; // EARLY EXIT!!!!!
    }

    final Object version;
    if ( persister.isVersioned() ) {
      final Object collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( lce.getKey(), persister );
      version = getLoadContext().getPersistenceContext().getEntry( collectionOwner ).getVersion();
    }
    else {
      version = null;
    }

    CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
    CacheKey cacheKey = new CacheKey(
        lce.getKey(),
        persister.getKeyType(),
        persister.getRole(),
        session.getEntityMode(),
        session.getFactory()
    );
    boolean put = persister.getCacheAccessStrategy().putFromLoad(
        cacheKey,
        persister.getCacheEntryStructure().structure(entry),
        session.getTimestamp(),
        version,
        factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH
    );

    if ( put && factory.getStatistics().isStatisticsEnabled() ) {
      factory.getStatisticsImplementor().secondLevelCachePut( persister.getCacheAccessStrategy().getRegion().getName() );
    }
View Full Code Here

  }

  public void execute() throws HibernateException {
   
    final EntityPersister persister = getPersister();
    final SessionImplementor session = getSession();
    final Object instance = getInstance();
   
    boolean veto = preInsert();

    // Don't need to lock the cache here, since if someone
    // else inserted the same pk first, the insert would fail

    if ( !veto ) {
      generatedId = persister.insert( state, instance, session );
      if ( persister.hasInsertGeneratedProperties() ) {
        persister.processInsertGeneratedProperties( generatedId, instance, state, session );
      }
      //need to do that here rather than in the save event listener to let
      //the post insert events to have a id-filled entity when IDENTITY is used (EJB3)
      persister.setIdentifier( instance, generatedId, session.getEntityMode() );
    }


    //TODO: this bit actually has to be called after all cascades!
    //      but since identity insert is called *synchronously*,
    //      instead of asynchronously as other actions, it isn't
    /*if ( persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
      cacheEntry = new CacheEntry(object, persister, session);
      persister.getCache().insert(generatedId, cacheEntry);
    }*/
   
    postInsert();

    if ( session.getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
      session.getFactory().getStatisticsImplementor()
          .insertEntity( getPersister().getEntityName() );
    }

  }
View Full Code Here

   * @param event The create event to be handled.
   * @throws HibernateException
   */
  public void onPersist(PersistEvent event, Map createCache) throws HibernateException {
     
    final SessionImplementor source = event.getSession();
    final Object object = event.getObject();
   
    final Object entity;
    if (object instanceof HibernateProxy) {
      LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
      if ( li.isUninitialized() ) {
        if ( li.getSession()==source ) {
          return; //NOTE EARLY EXIT!
        }
        else {
          throw new PersistentObjectException("uninitialized proxy passed to persist()");
        }
      }
      entity = li.getImplementation();
    }
    else {
      entity = object;
    }
   
    int entityState = getEntityState(
        entity,
        event.getEntityName(),
        source.getPersistenceContext().getEntry(entity),
        source
      );
   
    switch (entityState) {
      case DETACHED:
View Full Code Here

    this.emptySnapshot = emptySnapshot;
  }

  public void execute() throws HibernateException {
    final Serializable id = getKey();
    final SessionImplementor session = getSession();
    final CollectionPersister persister = getPersister();
    final PersistentCollection collection = getCollection();
    boolean affectedByFilters = persister.isAffectedByEnabledFilters(session);

    preUpdate();
View Full Code Here

   */
  public void onInitializeCollection(InitializeCollectionEvent event)
  throws HibernateException {

    PersistentCollection collection = event.getCollection();
    SessionImplementor source = event.getSession();

    CollectionEntry ce = source.getPersistenceContext().getCollectionEntry(collection);
    if (ce==null) throw new HibernateException("collection was evicted");
    if ( !collection.wasInitialized() ) {
      if ( log.isTraceEnabled() ) {
        log.trace(
            "initializing collection " +
            MessageHelper.collectionInfoString( ce.getLoadedPersister(), ce.getLoadedKey(), source.getFactory() )
          );
      }

      log.trace("checking second-level cache");
      final boolean foundInCache = initializeCollectionFromCache(
          ce.getLoadedKey(),
          ce.getLoadedPersister(),
          collection,
          source
        );

      if (foundInCache) {
        log.trace("collection initialized from cache");
      }
      else {
        log.trace("collection not cached");
        ce.getLoadedPersister().initialize( ce.getLoadedKey(), source );
        log.trace("collection initialized");

        if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
          source.getFactory().getStatisticsImplementor().fetchCollection(
              ce.getLoadedPersister().getRole()
            );
        }
      }
    }
View Full Code Here

  }

  public void execute() throws HibernateException {
    Serializable id = getId();
    EntityPersister persister = getPersister();
    SessionImplementor session = getSession();
    Object instance = getInstance();

    boolean veto = preUpdate();

    final SessionFactoryImplementor factory = getSession().getFactory();
    Object previousVersion = this.previousVersion;
    if ( persister.isVersionPropertyGenerated() ) {
      // we need to grab the version value from the entity, otherwise
      // we have issues with generated-version entities that may have
      // multiple actions queued during the same flush
      previousVersion = persister.getVersion( instance, session.getEntityMode() );
    }
   
    final CacheKey ck;
    if ( persister.hasCache() ) {
      ck = new CacheKey(
          id,
          persister.getIdentifierType(),
          persister.getRootEntityName(),
          session.getEntityMode(),
          session.getFactory()
      );
      lock = persister.getCacheAccessStrategy().lockItem( ck, previousVersion );
    }
    else {
      ck = null;
    }

    if ( !veto ) {
      persister.update(
          id,
          state,
          dirtyFields,
          hasDirtyCollection,
          previousState,
          previousVersion,
          instance,
          rowId,
          session
      );
    }

    EntityEntry entry = getSession().getPersistenceContext().getEntry( instance );
    if ( entry == null ) {
      throw new AssertionFailure( "possible nonthreadsafe access to session" );
    }
   
    if ( entry.getStatus()==Status.MANAGED || persister.isVersionPropertyGenerated() ) {
      // get the updated snapshot of the entity state by cloning current state;
      // it is safe to copy in place, since by this time no-one else (should have)
      // has a reference  to the array
      TypeFactory.deepCopy(
          state,
          persister.getPropertyTypes(),
          persister.getPropertyCheckability(),
          state,
          session
      );
      if ( persister.hasUpdateGeneratedProperties() ) {
        // this entity defines proeprty generation, so process those generated
        // values...
        persister.processUpdateGeneratedProperties( id, instance, state, session );
        if ( persister.isVersionPropertyGenerated() ) {
          nextVersion = Versioning.getVersion( state, persister );
        }
      }
      // have the entity entry perform post-update processing, passing it the
      // update state and the new version (if one).
      entry.postUpdate( instance, state, nextVersion );
    }

    if ( persister.hasCache() ) {
      if ( persister.isCacheInvalidationRequired() || entry.getStatus()!=Status.MANAGED ) {
        persister.getCacheAccessStrategy().remove( ck );
      }
      else {
        //TODO: inefficient if that cache is just going to ignore the updated state!
        CacheEntry ce = new CacheEntry(
            state,
            persister,
            persister.hasUninitializedLazyProperties( instance, session.getEntityMode() ),
            nextVersion,
            getSession(),
            instance
        );
        cacheEntry = persister.getCacheEntryStructure().structure( ce );
View Full Code Here

    super( session, key, owner );
  }

  Object processCollection(Object collection, CollectionType type) throws HibernateException {

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

    if ( collection == null ) {
      //do nothing
    }
    else if ( collection instanceof PersistentCollection ) {
View Full Code Here

  }

  public void execute() throws HibernateException {
    Serializable id = getId();
    EntityPersister persister = getPersister();
    SessionImplementor session = getSession();
    Object instance = getInstance();

    boolean veto = preDelete();

    Object version = this.version;
    if ( persister.isVersionPropertyGenerated() ) {
      // we need to grab the version value from the entity, otherwise
      // we have issues with generated-version entities that may have
      // multiple actions queued during the same flush
      version = persister.getVersion( instance, session.getEntityMode() );
    }

    final CacheKey ck;
    if ( persister.hasCache() ) {
      ck = new CacheKey(
          id,
          persister.getIdentifierType(),
          persister.getRootEntityName(),
          session.getEntityMode(),
          session.getFactory()
        );
      lock = persister.getCacheAccessStrategy().lockItem( ck, version );
    }
    else {
      ck = null;
    }

    if ( !isCascadeDeleteEnabled && !veto ) {
      persister.delete( id, version, instance, session );
    }
   
    //postDelete:
    // After actually deleting a row, record the fact that the instance no longer
    // exists on the database (needed for identity-column key generation), and
    // remove it from the session cache
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    EntityEntry entry = persistenceContext.removeEntry( instance );
    if ( entry == null ) {
      throw new AssertionFailure( "possible nonthreadsafe access to session" );
    }
    entry.postDelete();

    EntityKey key = new EntityKey( entry.getId(), entry.getPersister(), session.getEntityMode() );
    persistenceContext.removeEntity(key);
    persistenceContext.removeProxy(key);
   
    if ( persister.hasCache() ) persister.getCacheAccessStrategy().remove( ck );
View Full Code Here

TOP

Related Classes of org.hibernate.engine.SessionImplementor

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.