📄 statefulpersistencecontext.java
字号:
//check if the detached object being merged is the parent Object unmergedInstance = mergeMap.get( entityEntryInstance ); Object unmergedChild = mergeMap.get( childEntity ); if ( unmergedInstance != null && unmergedChild != null ) { found = isFoundInParent( propertyName, unmergedChild, persister, collectionPersister, unmergedInstance ); } } if ( found ) { return entityEntry.getId(); } } } // if we get here, it is possible that we have a proxy 'in the way' of the merge map resolution... // NOTE: decided to put this here rather than in the above loop as I was nervous about the performance // of the loop-in-loop especially considering this is far more likely the 'edge case' if ( mergeMap != null ) { Iterator mergeMapItr = mergeMap.entrySet().iterator(); while ( mergeMapItr.hasNext() ) { final Map.Entry mergeMapEntry = ( Map.Entry ) mergeMapItr.next(); if ( mergeMapEntry.getKey() instanceof HibernateProxy ) { final HibernateProxy proxy = ( HibernateProxy ) mergeMapEntry.getKey(); if ( persister.isSubclassEntityName( proxy.getHibernateLazyInitializer().getEntityName() ) ) { boolean found = isFoundInParent( propertyName, childEntity, persister, collectionPersister, mergeMap.get( proxy ) ); if ( !found ) { found = isFoundInParent( propertyName, mergeMap.get( childEntity ), persister, collectionPersister, mergeMap.get( proxy ) ); } if ( found ) { return proxy.getHibernateLazyInitializer().getIdentifier(); } } } } } return null; } private boolean isFoundInParent( String property, Object childEntity, EntityPersister persister, CollectionPersister collectionPersister, Object potentialParent) { Object collection = persister.getPropertyValue( potentialParent, property, session.getEntityMode() ); return collection != null && Hibernate.isInitialized( collection ) && collectionPersister.getCollectionType().contains( collection, childEntity, session ); } /** * Search the persistence context for an index of the child object, * given a collection role */ public Object getIndexInOwner(String entity, String property, Object childEntity, Map mergeMap) { EntityPersister persister = session.getFactory() .getEntityPersister(entity); CollectionPersister cp = session.getFactory() .getCollectionPersister(entity + '.' + property); 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 instance = me.getKey(); Object index = getIndexInParent(property, childEntity, persister, cp, instance); if (index==null && mergeMap!=null) { Object unmergedInstance = mergeMap.get(instance); Object unmergedChild = mergeMap.get(childEntity); if ( unmergedInstance!=null && unmergedChild!=null ) { index = getIndexInParent(property, unmergedChild, persister, cp, unmergedInstance); } } if (index!=null) return index; } } return null; } private Object getIndexInParent( String property, Object childEntity, EntityPersister persister, CollectionPersister collectionPersister, Object potentialParent ){ Object collection = persister.getPropertyValue( potentialParent, property, session.getEntityMode() ); if ( collection!=null && Hibernate.isInitialized(collection) ) { return collectionPersister.getCollectionType().indexOf(collection, childEntity); } else { return null; } } /** * Record the fact that the association belonging to the keyed * entity is null. */ public void addNullProperty(EntityKey ownerKey, String propertyName) { nullAssociations.add( new AssociationKey(ownerKey, propertyName) ); } /** * Is the association property belonging to the keyed entity null? */ public boolean isPropertyNull(EntityKey ownerKey, String propertyName) { return nullAssociations.contains( new AssociationKey(ownerKey, propertyName) ); } private void clearNullProperties() { nullAssociations.clear(); } public void setReadOnly(Object entity, boolean readOnly) { EntityEntry entry = getEntry(entity); if (entry==null) { throw new TransientObjectException("Instance was not associated with the session"); } entry.setReadOnly(readOnly, entity); hasNonReadOnlyEntities = hasNonReadOnlyEntities || !readOnly; } public void replaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, Serializable generatedId) { Object entity = entitiesByKey.remove( oldKey ); EntityEntry oldEntry = ( EntityEntry ) entityEntries.remove( entity ); EntityKey newKey = new EntityKey( generatedId, oldEntry.getPersister(), getSession().getEntityMode() ); addEntity( newKey, entity ); addEntry( entity, oldEntry.getStatus(), oldEntry.getLoadedState(), oldEntry.getRowId(), generatedId, oldEntry.getVersion(), oldEntry.getLockMode(), oldEntry.isExistsInDatabase(), oldEntry.getPersister(), oldEntry.isBeingReplicated(), oldEntry.isLoadedWithLazyPropertiesUnfetched() ); } /** * Used by the owning session to explicitly control serialization of the * persistence context. * * @param oos The stream to which the persistence context should get written * @throws IOException serialization errors. */ public void serialize(ObjectOutputStream oos) throws IOException { log.trace( "serializing persistent-context" ); oos.writeBoolean( hasNonReadOnlyEntities ); oos.writeInt( entitiesByKey.size() ); log.trace( "starting serialization of [" + entitiesByKey.size() + "] entitiesByKey entries" ); Iterator itr = entitiesByKey.entrySet().iterator(); while ( itr.hasNext() ) { Map.Entry entry = ( Map.Entry ) itr.next(); ( ( EntityKey ) entry.getKey() ).serialize( oos ); oos.writeObject( entry.getValue() ); } oos.writeInt( entitiesByUniqueKey.size() ); log.trace( "starting serialization of [" + entitiesByUniqueKey.size() + "] entitiesByUniqueKey entries" ); itr = entitiesByUniqueKey.entrySet().iterator(); while ( itr.hasNext() ) { Map.Entry entry = ( Map.Entry ) itr.next(); ( ( EntityUniqueKey ) entry.getKey() ).serialize( oos ); oos.writeObject( entry.getValue() ); } oos.writeInt( proxiesByKey.size() ); log.trace( "starting serialization of [" + proxiesByKey.size() + "] proxiesByKey entries" ); itr = proxiesByKey.entrySet().iterator(); while ( itr.hasNext() ) { Map.Entry entry = ( Map.Entry ) itr.next(); ( ( EntityKey ) entry.getKey() ).serialize( oos ); oos.writeObject( entry.getValue() ); } oos.writeInt( entitySnapshotsByKey.size() ); log.trace( "starting serialization of [" + entitySnapshotsByKey.size() + "] entitySnapshotsByKey entries" ); itr = entitySnapshotsByKey.entrySet().iterator(); while ( itr.hasNext() ) { Map.Entry entry = ( Map.Entry ) itr.next(); ( ( EntityKey ) entry.getKey() ).serialize( oos ); oos.writeObject( entry.getValue() ); } oos.writeInt( entityEntries.size() ); log.trace( "starting serialization of [" + entityEntries.size() + "] entityEntries entries" ); itr = entityEntries.entrySet().iterator(); while ( itr.hasNext() ) { Map.Entry entry = ( Map.Entry ) itr.next(); oos.writeObject( entry.getKey() ); ( ( EntityEntry ) entry.getValue() ).serialize( oos ); } oos.writeInt( collectionsByKey.size() ); log.trace( "starting serialization of [" + collectionsByKey.size() + "] collectionsByKey entries" ); itr = collectionsByKey.entrySet().iterator(); while ( itr.hasNext() ) { Map.Entry entry = ( Map.Entry ) itr.next(); ( ( CollectionKey ) entry.getKey() ).serialize( oos ); oos.writeObject( entry.getValue() ); } oos.writeInt( collectionEntries.size() ); log.trace( "starting serialization of [" + collectionEntries.size() + "] collectionEntries entries" ); itr = collectionEntries.entrySet().iterator(); while ( itr.hasNext() ) { Map.Entry entry = ( Map.Entry ) itr.next(); oos.writeObject( entry.getKey() ); ( ( CollectionEntry ) entry.getValue() ).serialize( oos ); } oos.writeInt( arrayHolders.size() ); log.trace( "starting serialization of [" + arrayHolders.size() + "] arrayHolders entries" ); itr = arrayHolders.entrySet().iterator(); while ( itr.hasNext() ) { Map.Entry entry = ( Map.Entry ) itr.next(); oos.writeObject( entry.getKey() ); oos.writeObject( entry.getValue() ); } oos.writeInt( nullifiableEntityKeys.size() ); log.trace( "starting serialization of [" + nullifiableEntityKeys.size() + "] nullifiableEntityKeys entries" ); itr = nullifiableEntityKeys.iterator(); while ( itr.hasNext() ) { EntityKey entry = ( EntityKey ) itr.next(); entry.serialize( oos ); } } public static StatefulPersistenceContext deserialize( ObjectInputStream ois, SessionImplementor session) throws IOException, ClassNotFoundException { log.trace( "deserializing persistent-context" ); StatefulPersistenceContext rtn = new StatefulPersistenceContext( session ); // during deserialization, we need to reconnect all proxies and // collections to this session, as well as the EntityEntry and // CollectionEntry instances; these associations are transient // because serialization is used for different things. try { // todo : we can actually just determine this from the incoming EntityEntry-s rtn.hasNonReadOnlyEntities = ois.readBoolean(); int count = ois.readInt(); log.trace( "staring deserialization of [" + count + "] entitiesByKey entries" ); rtn.entitiesByKey = new HashMap( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); for ( int i = 0; i < count; i++ ) { rtn.entitiesByKey.put( EntityKey.deserialize( ois, session ), ois.readObject() ); } count = ois.readInt(); log.trace( "staring deserialization of [" + count + "] entitiesByUniqueKey entries" ); rtn.entitiesByUniqueKey = new HashMap( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); for ( int i = 0; i < count; i++ ) { rtn.entitiesByUniqueKey.put( EntityUniqueKey.deserialize( ois, session ), ois.readObject() ); } count = ois.readInt(); log.trace( "staring deserialization of [" + count + "] proxiesByKey entries" ); rtn.proxiesByKey = new ReferenceMap( ReferenceMap.HARD, ReferenceMap.WEAK, count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count, .75f ); for ( int i = 0; i < count; i++ ) { EntityKey ek = EntityKey.deserialize( ois, session ); Object proxy = ois.readObject(); if ( proxy instanceof HibernateProxy ) { ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().setSession( session ); rtn.proxiesByKey.put( ek, proxy ); } else { log.trace( "encountered prunded proxy" ); } // otherwise, the proxy was pruned during the serialization process } count = ois.readInt(); log.trace( "staring deserialization of [" + count + "] entitySnapshotsByKey entries" ); rtn.entitySnapshotsByKey = new HashMap( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); for ( int i = 0; i < count; i++ ) { rtn.entitySnapshotsByKey.put( EntityKey.deserialize( ois, session ), ois.readObject() ); } count = ois.readInt(); log.trace( "staring deserialization of [" + count + "] entityEntries entries" ); rtn.entityEntries = IdentityMap.instantiateSequenced( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); for ( int i = 0; i < count; i++ ) { Object entity = ois.readObject(); EntityEntry entry = EntityEntry.deserialize( ois, session ); rtn.entityEntries.put( entity, entry ); } count = ois.readInt(); log.trace( "staring deserialization of [" + count + "] collectionsByKey entries" ); rtn.collectionsByKey = new HashMap( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); for ( int i = 0; i < count; i++ ) { rtn.collectionsByKey.put( CollectionKey.deserialize( ois, session ), ois.readObject() ); } count = ois.readInt(); log.trace( "staring deserialization of [" + count + "] collectionEntries entries" ); rtn.collectionEntries = IdentityMap.instantiateSequenced( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); for ( int i = 0; i < count; i++ ) { final PersistentCollection pc = ( PersistentCollection ) ois.readObject(); final CollectionEntry ce = CollectionEntry.deserialize( ois, session ); pc.setCurrentSession( session ); rtn.collectionEntries.put( pc, ce ); } count = ois.readInt(); log.trace( "staring deserialization of [" + count + "] arrayHolders entries" ); rtn.arrayHolders = IdentityMap.instantiate( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); for ( int i = 0; i < count; i++ ) { rtn.arrayHolders.put( ois.readObject(), ois.readObject() ); } count = ois.readInt(); log.trace( "staring deserialization of [" + count + "] nullifiableEntityKeys entries" ); rtn.nullifiableEntityKeys = new HashSet(); for ( int i = 0; i < count; i++ ) { rtn.nullifiableEntityKeys.add( EntityKey.deserialize( ois, session ) ); } } catch ( HibernateException he ) { throw new InvalidObjectException( he.getMessage() ); } return rtn; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -