Package org.hibernate.engine.spi

Examples of org.hibernate.engine.spi.SessionImplementor


   *
   * @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.debugf( "Caching collection: %s", 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.debugf( "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() ) {
      Object collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( lce.getKey(), persister );
      if ( collectionOwner == null ) {
        // generally speaking this would be caused by the collection key being defined by a property-ref, thus
        // the collection key and the owner key would not match up.  In this case, try to use the key of the
        // owner instance associated with the collection itself, if one.  If the collection does already know
        // about its owner, that owner should be the same instance as associated with the PC, but we do the
        // resolution against the PC anyway just to be safe since the lookup should not be costly.
        if ( lce.getCollection() != null ) {
          Object linkedOwner = lce.getCollection().getOwner();
          if ( linkedOwner != null ) {
            final Serializable ownerKey = persister.getOwnerEntityPersister().getIdentifier( linkedOwner, session );
            collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( ownerKey, persister );
          }
        }
        if ( collectionOwner == null ) {
          throw new HibernateException(
              "Unable to resolve owner of loading collection [" +
                  MessageHelper.collectionInfoString( persister, lce.getKey(), factory ) +
                  "] for second level caching"
          );
        }
      }
      version = getLoadContext().getPersistenceContext().getEntry( collectionOwner ).getVersion();
    }
    else {
      version = null;
    }

    CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
    CacheKey cacheKey = session.generateCacheKey( lce.getKey(), persister.getKeyType(), persister.getRole() );
    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


  @Override
  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 );
    }

    final CacheKey ck;
    if ( persister.hasCache() ) {
      ck = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() );
      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();
View Full Code Here

  }

  @Override
  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 );
      getSession().getPersistenceContext().registerInsertedKey( getPersister(), generatedId );
    }


    //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

  @Override
  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 );
    }
   
    final CacheKey ck;
    if ( persister.hasCache() ) {
      ck = session.generateCacheKey(
          id,
          persister.getIdentifierType(),
          persister.getRootEntityName()
      );
      lock = persister.getCacheAccessStrategy().lockItem( ck, previousVersion );
View Full Code Here

    return INVOKE_IMPLEMENTATION;

  }

  private Object getReplacement() {
    final SessionImplementor session = getSession();
    if ( isUninitialized() && session != null && session.isOpen()) {
      final EntityKey key = session.generateEntityKey(
          getIdentifier(),
          session.getFactory().getEntityPersister( getEntityName() )
      );
      final Object entity = session.getPersistenceContext().getEntity(key);
      if (entity!=null) setImplementation( entity );
    }

    if ( isUninitialized() ) {
      if (replacement==null) {
View Full Code Here

    return intercepted;
  }

  protected boolean handleInterception(FlushEntityEvent event) {
    SessionImplementor session = event.getSession();
    EntityEntry entry = event.getEntityEntry();
    EntityPersister persister = entry.getPersister();
    Object entity = event.getEntity();

    //give the Interceptor a chance to modify property values
View Full Code Here

   */
  protected void dirtyCheck(FlushEntityEvent event) throws HibernateException {

    final Object entity = event.getEntity();
    final Object[] values = event.getPropertyValues();
    final SessionImplementor session = event.getSession();
    final EntityEntry entry = event.getEntityEntry();
    final EntityPersister persister = entry.getPersister();
    final Serializable id = entry.getId();
    final Object[] loadedState = entry.getLoadedState();

    int[] dirtyProperties = session.getInterceptor().findDirty(
        entity,
        id,
        values,
        loadedState,
        persister.getPropertyNames(),
View Full Code Here

  }

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

    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 ) {
     
      persister.insert( id, state, instance, session );
   
      EntityEntry entry = session.getPersistenceContext().getEntry( instance );
      if ( entry == null ) {
        throw new AssertionFailure( "possible nonthreadsafe access to session" );
      }
     
      entry.postInsert();
 
      if ( persister.hasInsertGeneratedProperties() ) {
        persister.processInsertGeneratedProperties( id, instance, state, session );
        if ( persister.isVersionPropertyGenerated() ) {
          version = Versioning.getVersion( state, persister );
        }
        entry.postUpdate(instance, state, version);
      }

      getSession().getPersistenceContext().registerInsertedKey( getPersister(), getId() );
    }

    final SessionFactoryImplementor factory = getSession().getFactory();

    if ( isCachePutEnabled( persister, session ) ) {
     
      CacheEntry ce = new CacheEntry(
          state,
          persister,
          persister.hasUninitializedLazyProperties( instance ),
          version,
          session,
          instance
        );
     
      cacheEntry = persister.getCacheEntryStructure().structure(ce);
      final CacheKey ck = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() );
      boolean put = persister.getCacheAccessStrategy().insert( ck, cacheEntry, version );
     
      if ( put && factory.getStatistics().isStatisticsEnabled() ) {
        factory.getStatisticsImplementor().secondLevelCachePut( getPersister().getCacheAccessStrategy().getRegion().getName() );
      }
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;
    }

    final String entityName;
    if ( event.getEntityName() != null ) {
      entityName = event.getEntityName();
    }
    else {
      entityName = source.bestGuessEntityName( entity );
      event.setEntityName( entityName );
    }

    final EntityEntry entityEntry = source.getPersistenceContext().getEntry( entity );
    EntityState entityState = getEntityState( entity, entityName, entityEntry, source );
    if ( entityState == EntityState.DETACHED ) {
      // JPA 2, in its version of a "foreign generated", allows the id attribute value
      // to be manually set by the user, even though this manual value is irrelevant.
      // The issue is that this causes problems with the Hibernate unsaved-value strategy
      // which comes into play here in determining detached/transient state.
      //
      // Detect if we have this situation and if so null out the id value and calculate the
      // entity state again.

      // NOTE: entityEntry must be null to get here, so we cannot use any of its values
      EntityPersister persister = source.getFactory().getEntityPersister( entityName );
      if ( ForeignGenerator.class.isInstance( persister.getIdentifierGenerator() ) ) {
        if ( LOG.isDebugEnabled() && persister.getIdentifier( entity, source ) != null ) {
          LOG.debugf( "Resetting entity id attribute to null for foreign generator" );
        }
        persister.setIdentifier( entity, null, source );
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.tracev( "Initializing collection {0}",
            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

TOP

Related Classes of org.hibernate.engine.spi.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.