📄 persistencecontext.java
字号:
throws HibernateException { if ( cs.wasDereferenced() ) { addCollection(collection); } else { CollectionEntry ce = new CollectionEntry( cs, session.getFactory() ); collection.setCollectionSnapshot(ce); addCollection( collection, ce, cs.getKey(), em ); } } /** * add a collection we just pulled out of the cache (does not need initializing) */ public CollectionEntry addInitializedCollection(PersistentCollection collection, CollectionPersister persister, Serializable id, EntityMode em) throws HibernateException { CollectionEntry ce = new CollectionEntry(persister, id, flushing); ce.postInitialize(collection); collection.setCollectionSnapshot(ce); addCollection(collection, ce, id, em); return ce; } /** * Get the collection instance associated with the <tt>CollectionKey</tt> */ public PersistentCollection getCollection(CollectionKey collectionKey) { return (PersistentCollection) collectionsByKey.get(collectionKey); } /** * Register a collection for non-lazy loading at the end of the * two-phase load */ public void addNonLazyCollection(PersistentCollection collection) { nonlazyCollections.add(collection); } /** * Force initialization of all non-lazy collections encountered during * the current two-phase load (actually, this is a no-op, unless this * is the "outermost" load) */ public void initializeNonLazyCollections() throws HibernateException { if ( loadCounter == 0 ) { log.debug( "initializing non-lazy collections" ); //do this work only at the very highest level of the load loadCounter++; //don't let this method be called recursively try { int size; while ( ( size = nonlazyCollections.size() ) > 0 ) { //note that each iteration of the loop may add new elements ( (PersistentCollection) nonlazyCollections.remove( size - 1 ) ).forceInitialization(); } } finally { loadCounter--; clearNullProperties(); } } } /** * Get the <tt>PersistentCollection</tt> object for an array */ public PersistentCollection getCollectionHolder(Object array) { return (PersistentCollection) arrayHolders.get(array); } /** * 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 ); } public PersistentCollection removeCollectionHolder(Object array) { return (PersistentCollection) arrayHolders.remove(array); } /** * Get the snapshot of the pre-flush collection state */ public Serializable getSnapshot(PersistentCollection coll) { return getCollectionEntry(coll).getSnapshot(); } /** * Get the pre-flush identifier of the collection */ public Serializable getLoadedCollectionKey(PersistentCollection coll) { return getCollectionEntry(coll).getLoadedKey(); } /** * Is this the "inverse" end of a bidirectional association? */ public boolean isInverseCollection(PersistentCollection collection) { CollectionEntry ce = getCollectionEntry(collection); return ce != null && ce.getLoadedPersister().isInverse(); } /** * Get the collection entry for a collection passed to filter, * which might be a collection wrapper, an array, or an unwrapped * collection. Return null if there is no entry. */ public CollectionEntry getCollectionEntryOrNull(Object collection) { PersistentCollection coll; if ( collection instanceof PersistentCollection ) { coll = (PersistentCollection) collection; //if (collection==null) throw new TransientObjectException("Collection was not yet persistent"); } else { coll = getCollectionHolder(collection); if ( coll == null ) { //it might be an unwrapped collection reference! //try to find a wrapper (slowish) Iterator wrappers = IdentityMap.keyIterator(collectionEntries); while ( wrappers.hasNext() ) { PersistentCollection pc = (PersistentCollection) wrappers.next(); if ( pc.isWrapper(collection) ) { coll = pc; break; } } } } return (coll == null) ? null : getCollectionEntry(coll); } /** * Get an existing proxy by key */ public Object getProxy(EntityKey key) { return proxiesByKey.get(key); } /** * Add a proxy to the session cache */ public void addProxy(EntityKey key, Object proxy) { proxiesByKey.put(key, proxy); } /** * Remove a proxy from the session cache */ public Object removeProxy(EntityKey key) { return proxiesByKey.remove(key); } /** * Record the fact that an entity does not exist in the database * * @param key the primary key of the entity */ public void addNonExistantEntityKey(EntityKey key) { nonExistantEntityKeys.add(key); } /** * Record the fact that an entity does not exist in the database * * @param key a unique key of the entity */ public void addNonExistantEntityUniqueKey(EntityUniqueKey key) { nonExistentEntityUniqueKeys.add(key); } public void removeNonExist(EntityKey key) { nonExistantEntityKeys.remove(key); } /** * Retrieve the set of EntityKeys representing nullifiable references */ public HashSet getNullifiableEntityKeys() { return nullifiableEntityKeys; } public void setNullifiableEntityKeys(HashSet nullifiables) { // todo : yick !!! this.nullifiableEntityKeys = nullifiables; } public Map getEntitiesByKey() { return entitiesByKey; } public Map getEntityEntries() { return entityEntries; } public Map getCollectionEntries() { return collectionEntries; } public Map getCollectionsByKey() { return collectionsByKey; } /** * Do we already know that the entity does not exist in the * database? */ public boolean isNonExistant(EntityKey key) { return nonExistantEntityKeys.contains(key); } /** * Do we already know that the entity does not exist in the * database? */ public boolean isNonExistant(EntityUniqueKey key) { return nonExistentEntityUniqueKeys.contains(key); } public int getCascadeLevel() { return cascading; } public int incrementCascadeLevel() { return ++cascading; } public int decrementCascadeLevel() { return --cascading; } public boolean isFlushing() { return flushing; } public void setFlushing(boolean flushing) { this.flushing = flushing; } /** * Call this before begining a two-phase load */ public void beforeLoad() { loadCounter++; } /** * Call this after finishing a two-phase load */ public void afterLoad() { loadCounter--; } /** * Returns a string representation of the object. * * @return a string representation of the object. */ public String toString() { return new StringBuffer() .append("PersistentContext[entitiesByKey=") .append(entitiesByKey) .append("]") .toString(); } /** * Search the persistence context for an owner for the child object, * given a collection role */ public Serializable getOwnerId(String role, Object childObject) { int loc = role.lastIndexOf('.'); String entity = role.substring(0, loc); String property = role.substring( loc+1, role.length() ); EntityPersister persister = session.getFactory().getEntityPersister(entity); final CollectionPersister collectionPersister = session.getFactory().getCollectionPersister(role); CollectionType type = collectionPersister.getCollectionType(); Iterator entities = entityEntries.entrySet().iterator(); while ( entities.hasNext() ) { Map.Entry me = (Map.Entry) entities.next(); EntityEntry ee = (EntityEntry) me.getValue(); if ( persister.isSubclassEntityName( ee.getEntityName() ) ) { Object collection = persister.getPropertyValue( me.getKey(), property, session.getEntityMode() ); if ( collection!=null && type.contains(collection, childObject, collectionPersister, session) ) { return ee.getId(); } } } return null; } /** * Search the persistence context for an index of the child object, * given a collection role */ public Object getIndexInOwner(String role, Object childObject) { int loc = role.lastIndexOf('.'); String entity = role.substring(0, loc); String property = role.substring( loc+1, role.length() ); EntityPersister persister = session.getFactory().getEntityPersister(entity); CollectionPersister cp = session.getFactory().getCollectionPersister(role); Iterator entities = entityEntries.entrySet().iterator(); while ( entities.hasNext() ) { Map.Entry me = (Map.Entry) entities.next(); EntityEntry ee = (EntityEntry) me.getValue(); if ( persister.isSubclassEntityName( ee.getEntityName() ) ) { Object collection = persister.getPropertyValue( me.getKey(), property, session.getEntityMode() ); if ( collection!=null ) { if ( Hibernate.isInitialized(collection) ) { Object index = cp.getCollectionType().indexOf(collection, childObject); if (index!=null) return index; } } } } return null; } private static final class AssociationKey implements Serializable { private EntityKey ownerKey; private String propertyName; private AssociationKey(EntityKey ownerKey, String propertyName) { this.ownerKey = ownerKey; this.propertyName = propertyName; } public boolean equals(Object that) { AssociationKey key = (AssociationKey) that; return key.propertyName.equals(propertyName) && key.ownerKey.equals(ownerKey); } public int hashCode() { return ownerKey.hashCode() + propertyName.hashCode(); } } public void addNullProperty(EntityKey ownerKey, String propertyName) { nullAssociations.add( new AssociationKey(ownerKey, propertyName) ); } public boolean isPropertyNull(EntityKey ownerKey, String propertyName) { return nullAssociations.contains( new AssociationKey(ownerKey, propertyName) ); } public void clearNullProperties() { nullAssociations.clear(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -