Package org.hibernate.engine

Examples of org.hibernate.engine.PersistenceContext


   * @return The collection
   */
  public Object getCollection(Serializable key, SessionImplementor session, Object owner) {

    CollectionPersister persister = getPersister( session );
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final EntityMode entityMode = session.getEntityMode();

    if (entityMode==EntityMode.DOM4J && !isEmbeddedInXML) {
      return UNFETCHED_COLLECTION;
    }

    // check if collection is currently being loaded
    PersistentCollection collection = persistenceContext.getLoadContexts().locateLoadingCollection( persister, key );
    if ( collection == null ) {
      // check if it is already completely loaded, but unowned
      collection = persistenceContext.useUnownedCollection( new CollectionKey(persister, key, entityMode) );
      if ( collection == null ) {
        // create a new collection wrapper, to be initialized later
        collection = instantiate( session, persister, key );
        collection.setOwner( owner );

        persistenceContext.addUninitializedCollection( persister, collection, key );

        // some collections are not lazy:
        if ( initializeImmediately( entityMode ) ) {
          session.initializeCollection( collection, false );
        }
        else if ( !persister.isLazy() ) {
          persistenceContext.addNonLazyCollection( collection );
        }
 
        if ( hasHolder( entityMode ) ) {
          session.getPersistenceContext().addCollectionHolder( collection );
        }
View Full Code Here


    if ( !isSubselectLoadable() ) {
      return null;
    }
   
    final PersistenceContext persistenceContext = session.getPersistenceContext();
   
    SubselectFetch subselect = persistenceContext.getBatchFetchQueue()
      .getSubselect( new EntityKey( key, getOwnerEntityPersister(), session.getEntityMode() ) );
   
    if (subselect == null) {
      return null;
    }
    else {
     
      // Take care of any entities that might have
      // been evicted! 
      Iterator iter = subselect.getResult().iterator();
      while ( iter.hasNext() ) {
        if ( !persistenceContext.containsEntity( (EntityKey) iter.next() ) ) {
          iter.remove();
        }
      } 
     
      // Run a subquery loader
View Full Code Here

        getIdentifierOrUniqueKeyType( factory ),
        session.getEntityMode(),
        session.getFactory()
    );

    final PersistenceContext persistenceContext = session.getPersistenceContext();
    Object result = persistenceContext.getEntity( euk );
    if ( result == null ) {
      result = persister.loadByUniqueKey( uniqueKeyPropertyName, key, session );
    }
    return result == null ? null : persistenceContext.proxyFor( result );
  }
View Full Code Here

    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

   */
  protected void postFlush(SessionImplementor session) throws HibernateException {

    log.trace( "post flush" );

    final PersistenceContext persistenceContext = session.getPersistenceContext();
    persistenceContext.getCollectionsByKey().clear();
    persistenceContext.getBatchFetchQueue()
        .clearSubselects(); //the database has changed now, so the subselect results need to be invalidated

    Iterator iter = persistenceContext.getCollectionEntries().entrySet().iterator();
    while ( iter.hasNext() ) {
      Map.Entry me = (Map.Entry) iter.next();
      CollectionEntry collectionEntry = (CollectionEntry) me.getValue();
      PersistentCollection persistentCollection = (PersistentCollection) me.getKey();
      collectionEntry.postFlush(persistentCollection);
      if ( collectionEntry.getLoadedPersister() == null ) {
        //if the collection is dereferenced, remove from the session cache
        //iter.remove(); //does not work, since the entrySet is not backed by the set
        persistenceContext.getCollectionEntries()
            .remove(persistentCollection);
      }
      else {
        //otherwise recreate the mapping between the collection and its key
        CollectionKey collectionKey = new CollectionKey(
            collectionEntry.getLoadedPersister(),
            collectionEntry.getLoadedKey(),
            session.getEntityMode()
          );
        persistenceContext.getCollectionsByKey()
            .put(collectionKey, persistentCollection);
      }
    }
   
    session.getInterceptor().postFlush( new LazyIterator( persistenceContext.getEntitiesByKey() ) );

  }
View Full Code Here

   * @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

      return null;
    }
    else {
      CollectionPersister persister = session.getFactory().getCollectionPersister( collectionType.getRole() );

      final PersistenceContext persistenceContext = session.getPersistenceContext();
      //TODO: move into collection type, so we can use polymorphism!
      if ( collectionType.hasHolder( session.getEntityMode() ) ) {
       
        if (collection==CollectionType.UNFETCHED_COLLECTION) return null;

        PersistentCollection ah = persistenceContext.getCollectionHolder(collection);
        if (ah==null) {
          ah = collectionType.wrap(session, collection);
          persistenceContext.addNewCollection( persister, ah );
          persistenceContext.addCollectionHolder(ah);
        }
        return null;
      }
      else {

        PersistentCollection persistentCollection = collectionType.wrap(session, collection);
        persistenceContext.addNewCollection( persister, persistentCollection );

        if ( log.isTraceEnabled() ) log.trace( "Wrapped collection in role: " + collectionType.getRole() );

        return persistentCollection; //Force a substitution!
View Full Code Here

      else {

        CollectionCacheEntry cacheEntry = (CollectionCacheEntry) persister.getCacheEntryStructure()
            .destructure(ce, factory);
     
        final PersistenceContext persistenceContext = source.getPersistenceContext();
        cacheEntry.assemble(
            collection,
            persister, 
            persistenceContext.getCollectionOwner(id, persister)
          );
        persistenceContext.getCollectionEntry(collection).postInitialize(collection);
        //addInitializedCollection(collection, persister, id);
        return true;
      }
     
    }
View Full Code Here

   */
  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,
View Full Code Here

          "deleting " +
              MessageHelper.infoString( persister, entityEntry.getId(), session.getFactory() )
      );
    }

    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final Type[] propTypes = persister.getPropertyTypes();
    final Object version = entityEntry.getVersion();

    final Object[] currentState;
    if ( entityEntry.getLoadedState() == null ) { //ie. the entity came in from update()
      currentState = persister.getPropertyValues( entity, session.getEntityMode() );
    }
    else {
      currentState = entityEntry.getLoadedState();
    }

    final Object[] deletedState = createDeletedState( persister, currentState, session );
    entityEntry.setDeletedState( deletedState );

    session.getInterceptor().onDelete(
        entity,
        entityEntry.getId(),
        deletedState,
        persister.getPropertyNames(),
        propTypes
    );

    // before any callbacks, etc, so subdeletions see that this deletion happened first
    persistenceContext.setEntryStatus( entityEntry, Status.DELETED );
    EntityKey key = new EntityKey( entityEntry.getId(), persister, session.getEntityMode() );

    cascadeBeforeDelete( session, persister, entity, entityEntry, transientEntities );

    new ForeignKeys.Nullifier( entity, true, false, session )
        .nullifyTransientReferences( entityEntry.getDeletedState(), propTypes );
    new Nullability( session ).checkNullability( entityEntry.getDeletedState(), persister, true );
    persistenceContext.getNullifiableEntityKeys().add( key );

    // Ensures that containing deletions happen before sub-deletions
    session.getActionQueue().addAction(
        new EntityDeleteAction(
            entityEntry.getId(),
View Full Code Here

TOP

Related Classes of org.hibernate.engine.PersistenceContext

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.