Package org.hibernate.engine

Examples of org.hibernate.engine.PersistenceContext


  private List doQueryAndInitializeNonLazyCollections(final SessionImplementor session,
                            final QueryParameters queryParameters,
                            final boolean returnProxies)
    throws HibernateException, SQLException {

    final PersistenceContext persistenceContext = session.getPersistenceContext();
    persistenceContext.beforeLoad();
    List result;
    try {
      result = doQuery( session, queryParameters, returnProxies );
    }
    finally {
      persistenceContext.afterLoad();
    }
    persistenceContext.initializeNonLazyCollections();
    return result;
  }
View Full Code Here


        int owner = owners[i];
        if ( owner > -1 ) {
          EntityKey ownerKey = keys[owner];
          if ( keys[i] == null && ownerKey != null ) {
           
            final PersistenceContext persistenceContext = session.getPersistenceContext();
           
            /*final boolean isPrimaryKey;
            final boolean isSpecialOneToOne;
            if ( ownerAssociationTypes == null || ownerAssociationTypes[i] == null ) {
              isPrimaryKey = true;
              isSpecialOneToOne = false;
            }
            else {
              isPrimaryKey = ownerAssociationTypes[i].getRHSUniqueKeyPropertyName()==null;
              isSpecialOneToOne = ownerAssociationTypes[i].getLHSPropertyName()!=null;
            }*/
           
            //TODO: can we *always* use the "null property" approach for everything?
            /*if ( isPrimaryKey && !isSpecialOneToOne ) {
              persistenceContext.addNonExistantEntityKey(
                  new EntityKey( ownerKey.getIdentifier(), persisters[i], session.getEntityMode() )
              );
            }
            else if ( isSpecialOneToOne ) {*/
            boolean isOneToOneAssociation = ownerAssociationTypes!=null &&
                ownerAssociationTypes[i]!=null &&
                ownerAssociationTypes[i].isOneToOne();
            if ( isOneToOneAssociation ) {
              persistenceContext.addNullProperty( ownerKey,
                  ownerAssociationTypes[i].getPropertyName() );
            }
            /*}
            else {
              persistenceContext.addNonExistantEntityUniqueKey( new EntityUniqueKey(
View Full Code Here

          final CollectionAliases descriptor,
          final ResultSet rs,
          final SessionImplementor session)
  throws HibernateException, SQLException {

    final PersistenceContext persistenceContext = session.getPersistenceContext();

    final Serializable collectionRowKey = (Serializable) persister.readKey(
        rs,
        descriptor.getSuffixedKeyAliases(),
        session
      );
   
    if ( collectionRowKey != null ) {
      // we found a collection element in the result set

      if ( log.isDebugEnabled() ) {
        log.debug(
            "found row of collection: " +
            MessageHelper.collectionInfoString( persister, collectionRowKey, getFactory() )
          );
      }

      Object owner = optionalOwner;
      if ( owner == null ) {
        owner = persistenceContext.getCollectionOwner( collectionRowKey, persister );
        if ( owner == null ) {
          //TODO: This is assertion is disabled because there is a bug that means the
          //    original owner of a transient, uninitialized collection is not known
          //    if the collection is re-referenced by a different object associated
          //    with the current Session
          //throw new AssertionFailure("bug loading unowned collection");
        }
      }

      PersistentCollection rowCollection = persistenceContext.getLoadContexts()
          .getCollectionLoadContext( rs )
          .getLoadingCollection( persister, collectionRowKey );

      if ( rowCollection != null ) {
        rowCollection.readFrom( rs, persister, descriptor, owner );
      }

    }
    else if ( optionalKey != null ) {
      // we did not find a collection element in the result set, so we
      // ensure that a collection is created with the owner's identifier,
      // since what we have is an empty collection

      if ( log.isDebugEnabled() ) {
        log.debug(
            "result set contains (possibly empty) collection: " +
            MessageHelper.collectionInfoString( persister, optionalKey, getFactory() )
          );
      }

      persistenceContext.getLoadContexts()
          .getCollectionLoadContext( rs )
          .getLoadingCollection( persister, optionalKey ); // handle empty collection

    }
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

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

    postDelete();
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

        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

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.