Package org.hibernate.engine.spi

Examples of org.hibernate.engine.spi.CollectionEntry


    PersistentCollection collection = (PersistentCollection) pc;
    if ( collection.unsetSession( getSession() ) ) evictCollection(collection);
  }

  private void evictCollection(PersistentCollection collection) {
    CollectionEntry ce = (CollectionEntry) getSession().getPersistenceContext().getCollectionEntries().remove(collection);
    if ( LOG.isDebugEnabled() ) {
      LOG.debugf( "Evicting collection: %s",
          MessageHelper.collectionInfoString( ce.getLoadedPersister(),
              collection,
              ce.getLoadedKey(),
              getSession() ) );
    }
    if ( ce.getLoadedPersister() != null && ce.getLoadedPersister().getBatchSize() > 1 ) {
      getSession().getPersistenceContext().getBatchFetchQueue().removeBatchLoadableCollection( ce );
    }
    if ( ce.getLoadedPersister() != null && ce.getLoadedKey() != null ) {
      //TODO: is this 100% correct?
      getSession().getPersistenceContext().getCollectionsByKey().remove(
          new CollectionKey( ce.getLoadedPersister(), ce.getLoadedKey() )
      );
    }
  }
View Full Code Here


  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() ) {
      final boolean traceEnabled = LOG.isTraceEnabled();
      if ( traceEnabled ) {
        LOG.tracev( "Initializing collection {0}",
            MessageHelper.collectionInfoString( ce.getLoadedPersister(), collection, ce.getLoadedKey(), source ) );
        LOG.trace( "Checking second-level cache" );
      }

      final boolean foundInCache = initializeCollectionFromCache(
          ce.getLoadedKey(),
          ce.getLoadedPersister(),
          collection,
          source
        );

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

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

   */
  private void deleteOrphans(String entityName, PersistentCollection pc) throws HibernateException {
    //TODO: suck this logic into the collection!
    final Collection orphans;
    if ( pc.wasInitialized() ) {
      CollectionEntry ce = eventSource.getPersistenceContext().getCollectionEntry(pc);
      orphans = ce==null ?
          java.util.Collections.EMPTY_LIST :
          ce.getOrphans(entityName, pc);
    }
    else {
      orphans = pc.getQueuedOrphans(entityName);
    }

View Full Code Here

    }
  }

  private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session) {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    CollectionEntry entry = persistenceContext.getCollectionEntry(coll);
    final CollectionPersister loadedPersister = entry.getLoadedPersister();

    if ( loadedPersister != null && LOG.isDebugEnabled() ) {
      LOG.debugf(
          "Collection dereferenced: %s",
          MessageHelper.collectionInfoString( loadedPersister,
              coll, entry.getLoadedKey(), session
          )
      );
    }

    // do a check
    boolean hasOrphanDelete = loadedPersister != null && loadedPersister.hasOrphanDelete();
    if (hasOrphanDelete) {
      Serializable ownerId = loadedPersister.getOwnerEntityPersister().getIdentifier( coll.getOwner(), session );
      if ( ownerId == null ) {
        // the owning entity may have been deleted and its identifier unset due to
        // identifier-rollback; in which case, try to look up its identifier from
        // the persistence context
        if ( session.getFactory().getSettings().isIdentifierRollbackEnabled() ) {
          EntityEntry ownerEntry = persistenceContext.getEntry( coll.getOwner() );
          if ( ownerEntry != null ) {
            ownerId = ownerEntry.getId();
          }
        }
        if ( ownerId == null ) {
          throw new AssertionFailure( "Unable to determine collection owner identifier for orphan-delete processing" );
        }
      }
      EntityKey key = session.generateEntityKey( ownerId, loadedPersister.getOwnerEntityPersister() );
      Object owner = persistenceContext.getEntity(key);
      if ( owner == null ) {
        throw new AssertionFailure(
            "collection owner not associated with session: " +
            loadedPersister.getRole()
        );
      }
      EntityEntry e = persistenceContext.getEntry(owner);
      //only collections belonging to deleted entities are allowed to be dereferenced in the case of orphan delete
      if ( e != null && e.getStatus() != Status.DELETED && e.getStatus() != Status.GONE ) {
        throw new HibernateException(
            "A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: " +
            loadedPersister.getRole()
        );
      }
    }

    // do the work
    entry.setCurrentPersister(null);
    entry.setCurrentKey(null);
    prepareCollectionForUpdate( coll, entry, session.getFactory() );

  }
View Full Code Here

  private static void processNeverReferencedCollection(PersistentCollection coll, SessionImplementor session)
  throws HibernateException {

    final PersistenceContext persistenceContext = session.getPersistenceContext();
    CollectionEntry entry = persistenceContext.getCollectionEntry(coll);

    if ( LOG.isDebugEnabled() ) {
      LOG.debugf( "Found collection with unloaded owner: %s",
          MessageHelper.collectionInfoString(
              entry.getLoadedPersister(), coll,
              entry.getLoadedKey(), session ) );
    }

    entry.setCurrentPersister( entry.getLoadedPersister() );
    entry.setCurrentKey( entry.getLoadedKey() );

    prepareCollectionForUpdate( coll, entry, session.getFactory() );

  }
View Full Code Here

          Object entity,
          SessionImplementor session) {

    collection.setOwner(entity);

    CollectionEntry ce = session.getPersistenceContext().getCollectionEntry(collection);

    if ( ce == null ) {
      // refer to comment in StatefulPersistenceContext.addCollection()
      throw new HibernateException(
          "Found two representations of same collection: " +
          type.getRole()
      );
    }

    // The CollectionEntry.isReached() stuff is just to detect any silly users
    // who set up circular or shared references between/to collections.
    if ( ce.isReached() ) {
      // We've been here before
      throw new HibernateException(
          "Found shared references to a collection: " +
          type.getRole()
      );
    }
    ce.setReached(true);

    SessionFactoryImplementor factory = session.getFactory();
    CollectionPersister persister = factory.getCollectionPersister( type.getRole() );
    ce.setCurrentPersister(persister);
    ce.setCurrentKey( type.getKeyOfOwner(entity, session) ); //TODO: better to pass the id in as an argument?

        if (LOG.isDebugEnabled()) {
            if (collection.wasInitialized()) LOG.debugf("Collection found: %s, was: %s (initialized)",
                                                        MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session),
                                                        MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection,
                                                                                           ce.getLoadedKey(),
                                                                                           session));
            else LOG.debugf("Collection found: %s, was: %s (uninitialized)",
                            MessageHelper.collectionInfoString(persister, collection, ce.getCurrentKey(), session),
                            MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), session));
        }

    prepareCollectionForUpdate( collection, ce, factory );

  }
View Full Code Here

    if ( persister.getCollectionType().hasHolder() ) {
      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() ) {
View Full Code Here

      boolean shallow) throws HibernateException {
    if ( collection == null ) {
      throw new NullPointerException( "null collection passed to filter" );
    }

    CollectionEntry entry = persistenceContext.getCollectionEntryOrNull( collection );
    final CollectionPersister roleBeforeFlush = (entry == null) ? null : entry.getLoadedPersister();

    FilterQueryPlan plan = null;
    if ( roleBeforeFlush == null ) {
      // if it was previously unreferenced, we need to flush in order to
      // get its state into the database in order to execute query
      flush();
      entry = persistenceContext.getCollectionEntryOrNull( collection );
      CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
      if ( roleAfterFlush == null ) {
        throw new QueryException( "The collection was unreferenced" );
      }
      plan = factory.getQueryPlanCache().getFilterQueryPlan( filter, roleAfterFlush.getRole(), shallow, getEnabledFilters() );
    }
    else {
      // otherwise, we only need to flush if there are in-memory changes
      // to the queried tables
      plan = factory.getQueryPlanCache().getFilterQueryPlan( filter, roleBeforeFlush.getRole(), shallow, getEnabledFilters() );
      if ( autoFlushIfRequired( plan.getQuerySpaces() ) ) {
        // might need to run a different filter entirely after the flush
        // because the collection role may have changed
        entry = persistenceContext.getCollectionEntryOrNull( collection );
        CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
        if ( roleBeforeFlush != roleAfterFlush ) {
          if ( roleAfterFlush == null ) {
            throw new QueryException( "The collection was dereferenced" );
          }
          plan = factory.getQueryPlanCache().getFilterQueryPlan( filter, roleAfterFlush.getRole(), shallow, getEnabledFilters() );
        }
      }
    }

    if ( parameters != null ) {
      parameters.getPositionalParameterValues()[0] = entry.getLoadedKey();
      parameters.getPositionalParameterTypes()[0] = entry.getLoadedPersister().getKeyType();
    }

    return plan;
  }
View Full Code Here

    super( enversConfiguration );
  }

  @Override
  public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
        CollectionEntry collectionEntry = getCollectionEntry( event );
        if ( ! collectionEntry.getLoadedPersister().isInverse() ) {
            onCollectionAction( event, event.getCollection(), null, collectionEntry );
        }
  }
View Full Code Here

    super( enversConfiguration );
  }

  @Override
  public void onPreUpdateCollection(PreCollectionUpdateEvent event) {
        CollectionEntry collectionEntry = getCollectionEntry( event );
        if ( ! collectionEntry.getLoadedPersister().isInverse() ) {
            onCollectionAction( event, event.getCollection(), collectionEntry.getSnapshot(), collectionEntry );
        }
  }
View Full Code Here

TOP

Related Classes of org.hibernate.engine.spi.CollectionEntry

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.