private static boolean collectionIsInitialized(Object collection) { return !(collection instanceof PersistentCollection) || ( (PersistentCollection) collection ).wasInitialized(); }
protected Object findLazyValue(PersistentCollection coll) { // If lazy-loaded, not yet loaded, may serialize as null? if (!Feature.FORCE_LAZY_LOADING.enabledIn(_features) && !coll.wasInitialized()) { return null; } if(_sessionFactory != null) { Session session = openTemporarySessionForLoading(coll); initializeCollection(coll, session); } return coll.getValue(); }
public CollectionCacheEntry(PersistentCollection collection, CollectionPersister persister) { this.state = collection.disassemble(persister); }
public void assemble( final PersistentCollection collection, final CollectionPersister persister, final Object owner ) { collection.initializeFromCache(persister, state, owner); collection.afterInitialize(); }
/** * Determine if the collection is "really" dirty, by checking dirtiness * of the collection elements, if necessary */ private void dirty(PersistentCollection collection) throws HibernateException { boolean forceDirty = collection.wasInitialized() && !collection.isDirty() && //optimization getLoadedPersister() != null && getLoadedPersister().isMutable() && //optimization ( collection.isDirectlyAccessible() || getLoadedPersister().getElementType().isMutable() ) && //optimization !collection.equalsSnapshot( getLoadedPersister() ); if ( forceDirty ) { collection.dirty(); } }
public void execute() throws HibernateException { Serializable id = getKey(); SessionImplementor session = getSession(); CollectionPersister persister = getPersister(); if ( !collection.wasInitialized() ) { if ( !collection.hasQueuedAdditions() ) throw new AssertionFailure( "no queued adds" ); //do nothing - we only need to notify the cache... } else if ( collection.empty() ) { if ( !emptySnapshot ) persister.remove( id, session ); } else if ( collection.needsRecreate( getPersister() ) ) { if ( !emptySnapshot ) persister.remove( id, session ); persister.recreate( collection, id, session ); } else { persister.deleteRows( collection, id, session ); persister.updateRows( collection, id, session ); persister.insertRows( collection, id, session ); } evict(); if ( getSession().getFactory().getStatistics().isStatisticsEnabled() ) { getSession().getFactory().getStatisticsImplementor(). updateCollection( getPersister().getRole() ); } }
Object processCollection(Object collection, CollectionType type) throws HibernateException { if (collection==CollectionType.UNFETCHED_COLLECTION) return null; SessionImplementor session = getSession(); Serializable key = getKey(); CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() ); if (isUpdate) removeCollection(persister, key, session); if ( collection!=null && (collection instanceof PersistentCollection) ) { PersistentCollection wrapper = (PersistentCollection) collection; wrapper.setCurrentSession(session); if ( wrapper.wasInitialized() ) { session.getPersistenceContext().addNewCollection(wrapper, persister); } else { reattachCollection( wrapper, wrapper.getCollectionSnapshot() ); } } else { // otherwise a null or brand new collection // this will also (inefficiently) handle arrays, which // have no snapshot, so we can't do any better //processArrayOrNewCollection(collection, type); } return null; }
if ( collection.wasInitialized() ) { log.trace( "collection already initialized: ignoring" ); return null; //ignore this row of results! Note the early exit collection.beforeInitialize( persister, -1 ); collection.beginRead(); addLoadingCollectionEntry(ckey, collection, persister, resultSetId); return collection;
boolean affectedByFilters = persister.isAffectedByEnabledFilters(session); if ( !collection.wasInitialized() ) { if ( !collection.hasQueuedOperations() ) throw new AssertionFailure( "no queued adds" ); else if ( !affectedByFilters && collection.empty() ) { if ( !emptySnapshot ) persister.remove( id, session ); else if ( collection.needsRecreate(persister) ) { if (affectedByFilters) { throw new HibernateException(
/** * Called after execution of an action */ public void afterAction(PersistentCollection collection) { loadedKey = getCurrentKey(); setLoadedPersister( getCurrentPersister() ); boolean resnapshot = collection.wasInitialized() && ( isDoremove() || isDorecreate() || isDoupdate() ); if ( resnapshot ) { snapshot = loadedPersister==null || !loadedPersister.isMutable() ? null : collection.getSnapshot(loadedPersister); //re-snapshot } collection.postAction(); }
Object processCollection(Object collection, CollectionType type) throws HibernateException { if (collection==CollectionType.UNFETCHED_COLLECTION) return null; EventSource session = getSession(); Serializable key = getKey(); CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() ); if (isUpdate) removeCollection(persister, key, session); if ( collection!=null && (collection instanceof PersistentCollection) ) { PersistentCollection wrapper = (PersistentCollection) collection; wrapper.setCurrentSession(session); if ( wrapper.wasInitialized() ) { session.getPersistenceContext().addNewCollection(persister, wrapper); } else { reattachCollection( wrapper, type ); } } else { // otherwise a null or brand new collection // this will also (inefficiently) handle arrays, which // have no snapshot, so we can't do any better //processArrayOrNewCollection(collection, type); } return null; }
public boolean isSnapshotEmpty(PersistentCollection collection) { //TODO: does this really need to be here? // does the collection already have // it's own up-to-date snapshot? return collection.wasInitialized() && ( getLoadedPersister()==null || getLoadedPersister().isMutable() ) && collection.isSnapshotEmpty( getSnapshot() ); }
/** * Delete any entities that were removed from the collection */ 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 ? CollectionHelper.EMPTY_COLLECTION : ce.getOrphans(entityName, pc); } else { orphans = pc.getQueuedOrphans(entityName); } final Iterator orphanIter = orphans.iterator(); while ( orphanIter.hasNext() ) { Object orphan = orphanIter.next(); if (orphan!=null) { if ( log.isTraceEnabled() ) { log.trace("deleting orphaned entity instance: " + entityName); } eventSource.delete( entityName, orphan, false, null ); } } }
/** * Reattach a detached (disassociated) initialized or uninitialized * collection wrapper, using a snapshot carried with the collection * wrapper */ protected void reattachCollection(PersistentCollection collection, CollectionType type) throws HibernateException { if ( collection.wasInitialized() ) { CollectionPersister collectionPersister = getSession().getFactory() .getCollectionPersister( type.getRole() ); getSession().getPersistenceContext() .addInitializedDetachedCollection( collectionPersister, collection ); } else { if ( !isCollectionSnapshotValid(collection) ) { throw new HibernateException( "could not reassociate uninitialized transient collection" ); } CollectionPersister collectionPersister = getSession().getFactory() .getCollectionPersister( collection.getRole() ); getSession().getPersistenceContext() .addUninitializedDetachedCollection( collectionPersister, collection ); } }
throws HibernateException { collection.setOwner(entity); ", was: " + MessageHelper.collectionInfoString( ce.getLoadedPersister(), ce.getLoadedKey(), factory ) + ( collection.wasInitialized() ? " (initialized)" : " (uninitialized)" ) );
/** * record the fact that this collection was dereferenced * * @param coll The collection to be updated by unreachability. * @throws HibernateException */ public static void processUnreachableCollection(PersistentCollection coll, SessionImplementor session) throws HibernateException { if ( coll.getOwner()==null ) { processNeverReferencedCollection(coll, session); } else { processDereferencedCollection(coll, session); } }
/** * Register a <tt>PersistentCollection</tt> object for an array. * Associates a holder with an array - MUST be called after loading * array, since the array instance is not created until endLoad(). */ public void addCollectionHolder(PersistentCollection holder) { //TODO:refactor + make this method private arrayHolders.put( holder.getValue(), holder ); }
private void initializeCollection(PersistentCollection coll, Session session) { SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl)((SessionImplementor) session).getFactory(); TransactionFactory transactionFactory = sessionFactoryImpl.getTransactionFactory(); boolean isJTA = true; if(transactionFactory instanceof JDBCTransactionFactory) { isJTA = false; } if (!isJTA) { session.beginTransaction(); } coll.setCurrentSession(((SessionImplementor) session)); Hibernate.initialize(coll); if (!isJTA) { session.getTransaction().commit(); } session.close(); }
Object processCollection(Object collection, CollectionType collectionType) throws HibernateException { if ( collection!=null && (collection instanceof PersistentCollection) ) { final SessionImplementor session = getSession(); PersistentCollection coll = (PersistentCollection) collection; if ( coll.setCurrentSession(session) ) { reattachCollection( coll, coll.getCollectionSnapshot() ); } return null; } else { return processArrayOrNewCollection(collection, collectionType); } }