Package org.hibernate.engine

Examples of org.hibernate.engine.PersistenceContext


        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

    if ( session.getCacheMode().isGetEnabled() ) {
      boolean isImmutableNaturalKeyLookup = queryParameters.isNaturalKeyLookup()
          && getEntityPersisters()[0].getEntityMetamodel().hasImmutableNaturalId();

      final PersistenceContext persistenceContext = session.getPersistenceContext();
      boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
      if ( queryParameters.isReadOnlyInitialized() ) {
        // The read-only/modifiable mode for the query was explicitly set.
        // Temporarily set the default read-only/modifiable setting to the query's setting.
        persistenceContext.setDefaultReadOnly( queryParameters.isReadOnly() );
      }
      else {
        // The read-only/modifiable setting for the query was not initialized.
        // Use the default read-only/modifiable from the persistence context instead.
        queryParameters.setReadOnly( persistenceContext.isDefaultReadOnly() );
      }
      try {
        result = queryCache.get( key, resultTypes, isImmutableNaturalKeyLookup, querySpaces, session );
        logCachedResultDetails(
            key.getResultTransformer(),
            resultTypes,
            result
        );
      }
      finally {
        persistenceContext.setDefaultReadOnly( defaultReadOnlyOrig );
      }

      // If there is a result transformer, but the loader is not expecting the data to be
      // transformed yet, then the loader expects result elements that are Object[].
      // The problem is that StandardQueryCache.get(...) does not return a tuple when
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

   */
  private List doQueryAndInitializeNonLazyCollections(
      final SessionImplementor session,
      final QueryParameters queryParameters,
      final boolean returnProxies) throws HibernateException, SQLException {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
    if ( queryParameters.isReadOnlyInitialized() ) {
      // The read-only/modifiable mode for the query was explicitly set.
      // Temporarily set the default read-only/modifiable setting to the query's setting.
      persistenceContext.setDefaultReadOnly( queryParameters.isReadOnly() );
    }
    else {
      // The read-only/modifiable setting for the query was not initialized.
      // Use the default read-only/modifiable from the persistence context instead.
      queryParameters.setReadOnly( persistenceContext.isDefaultReadOnly() );
    }
    persistenceContext.beforeLoad();
    List result;
    try {
      try {
        result = doQuery( session, queryParameters, returnProxies );
      }
      finally {
        persistenceContext.afterLoad();
      }
      persistenceContext.initializeNonLazyCollections();
    }
    finally {
      // Restore the original default
      persistenceContext.setDefaultReadOnly( defaultReadOnlyOrig );
    }
    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

    if ( session.getCacheMode().isGetEnabled() ) {
      boolean isImmutableNaturalKeyLookup = queryParameters.isNaturalKeyLookup()
          && getEntityPersisters()[0].getEntityMetamodel().hasImmutableNaturalId();

      final PersistenceContext persistenceContext = session.getPersistenceContext();
      boolean defaultReadOnlyOrig = persistenceContext.isDefaultReadOnly();
      if ( queryParameters.isReadOnlyInitialized() ) {
        // The read-only/modifiable mode for the query was explicitly set.
        // Temporarily set the default read-only/modifiable setting to the query's setting.
        persistenceContext.setDefaultReadOnly( queryParameters.isReadOnly() );
      }
      else {
        // The read-only/modifiable setting for the query was not initialized.
        // Use the default read-only/modifiable from the persistence context instead.
        queryParameters.setReadOnly( persistenceContext.isDefaultReadOnly() );
      }
      try {
        result = queryCache.get( key, resultTypes, isImmutableNaturalKeyLookup, querySpaces, session );
      }
      finally {
        persistenceContext.setDefaultReadOnly( defaultReadOnlyOrig );
      }

      if ( factory.getStatistics().isStatisticsEnabled() ) {
        if ( result == null ) {
          factory.getStatisticsImplementor()
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

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.