Method from org.hibernate.engine.StatefulPersistenceContext Detail: |
public void addCollectionHolder(PersistentCollection holder) {
//TODO:refactor + make this method private
arrayHolders.put( holder.getValue(), holder );
}
Register a PersistentCollection 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 addEntity(EntityKey key,
Object entity) {
entitiesByKey.put(key, entity);
getBatchFetchQueue().removeBatchLoadableEntityKey(key);
}
|
public void addEntity(EntityUniqueKey euk,
Object entity) {
entitiesByUniqueKey.put(euk, entity);
}
Add an entity to the cache by unique key |
public EntityEntry addEntity(Object entity,
Status status,
Object[] loadedState,
EntityKey entityKey,
Object version,
LockMode lockMode,
boolean existsInDatabase,
EntityPersister persister,
boolean disableVersionIncrement,
boolean lazyPropertiesAreUnfetched) {
addEntity( entityKey, entity );
return addEntry(
entity,
status,
loadedState,
null,
entityKey.getIdentifier(),
version,
lockMode,
existsInDatabase,
persister,
disableVersionIncrement,
lazyPropertiesAreUnfetched
);
}
Adds an entity to the internal caches. |
public EntityEntry addEntry(Object entity,
Status status,
Object[] loadedState,
Object rowId,
Serializable id,
Object version,
LockMode lockMode,
boolean existsInDatabase,
EntityPersister persister,
boolean disableVersionIncrement,
boolean lazyPropertiesAreUnfetched) {
EntityEntry e = new EntityEntry(
status,
loadedState,
rowId,
id,
version,
lockMode,
existsInDatabase,
persister,
session.getEntityMode(),
disableVersionIncrement,
lazyPropertiesAreUnfetched
);
entityEntries.put(entity, e);
setHasNonReadOnlyEnties(status);
return e;
}
Generates an appropriate EntityEntry instance and adds it
to the event source's internal caches. |
public CollectionEntry addInitializedCollection(CollectionPersister persister,
PersistentCollection collection,
Serializable id) throws HibernateException {
CollectionEntry ce = new CollectionEntry(collection, persister, id, flushing);
ce.postInitialize(collection);
addCollection(collection, ce, id);
return ce;
}
add a collection we just pulled out of the cache (does not need initializing) |
public void addInitializedDetachedCollection(CollectionPersister collectionPersister,
PersistentCollection collection) throws HibernateException {
if ( collection.isUnreferenced() ) {
//treat it just like a new collection
addCollection( collection, collectionPersister );
}
else {
CollectionEntry ce = new CollectionEntry( collection, session.getFactory() );
addCollection( collection, ce, collection.getKey() );
}
}
add an (initialized) collection that was created by another session and passed
into update() (ie. one with a snapshot and existing state on the database) |
public void addNewCollection(CollectionPersister persister,
PersistentCollection collection) throws HibernateException {
addCollection(collection, persister);
}
Add a new collection (ie. a newly created one, just instantiated by the
application, with no database state or snapshot) |
public void addNonLazyCollection(PersistentCollection collection) {
nonlazyCollections.add(collection);
}
Register a collection for non-lazy loading at the end of the
two-phase load |
public void addNullProperty(EntityKey ownerKey,
String propertyName) {
nullAssociations.add( new AssociationKey(ownerKey, propertyName) );
}
Record the fact that the association belonging to the keyed
entity is null. |
public void addProxy(EntityKey key,
Object proxy) {
proxiesByKey.put(key, proxy);
}
Add a proxy to the session cache |
public void addUninitializedCollection(CollectionPersister persister,
PersistentCollection collection,
Serializable id) {
CollectionEntry ce = new CollectionEntry(collection, persister, id, flushing);
addCollection(collection, ce, id);
}
add a collection we just loaded up (still needs initializing) |
public void addUninitializedDetachedCollection(CollectionPersister persister,
PersistentCollection collection) {
CollectionEntry ce = new CollectionEntry( persister, collection.getKey() );
addCollection( collection, ce, collection.getKey() );
}
add a detached uninitialized collection |
public void addUnownedCollection(CollectionKey key,
PersistentCollection collection) {
if (unownedCollections==null) {
unownedCollections = new HashMap(8);
}
unownedCollections.put(key, collection);
}
|
public void afterLoad() {
loadCounter--;
}
Call this after finishing a two-phase load |
public void afterTransactionCompletion() {
// Downgrade locks
Iterator iter = entityEntries.values().iterator();
while ( iter.hasNext() ) {
( (EntityEntry) iter.next() ).setLockMode(LockMode.NONE);
}
}
|
public void beforeLoad() {
loadCounter++;
}
Call this before begining a two-phase load |
public void checkUniqueness(EntityKey key,
Object object) throws HibernateException {
Object entity = getEntity(key);
if ( entity == object ) {
throw new AssertionFailure( "object already associated, but no entry was found" );
}
if ( entity != null ) {
throw new NonUniqueObjectException( key.getIdentifier(), key.getEntityName() );
}
}
Attempts to check whether the given key represents an entity already loaded within the
current session. |
public void clear() {
Iterator itr = proxiesByKey.values().iterator();
while ( itr.hasNext() ) {
final LazyInitializer li = ( ( HibernateProxy ) itr.next() ).getHibernateLazyInitializer();
li.unsetSession();
}
Map.Entry[] collectionEntryArray = IdentityMap.concurrentEntries( collectionEntries );
for ( int i = 0; i < collectionEntryArray.length; i++ ) {
( ( PersistentCollection ) collectionEntryArray[i].getKey() ).unsetSession( getSession() );
}
arrayHolders.clear();
entitiesByKey.clear();
entitiesByUniqueKey.clear();
entityEntries.clear();
entitySnapshotsByKey.clear();
collectionsByKey.clear();
collectionEntries.clear();
if ( unownedCollections != null ) {
unownedCollections.clear();
}
proxiesByKey.clear();
nullifiableEntityKeys.clear();
if ( batchFetchQueue != null ) {
batchFetchQueue.clear();
}
hasNonReadOnlyEntities = false;
if ( loadContexts != null ) {
loadContexts.cleanup();
}
}
|
public boolean containsCollection(PersistentCollection collection) {
return collectionEntries.containsKey(collection);
}
|
public boolean containsEntity(EntityKey key) {
return entitiesByKey.containsKey(key);
}
|
public boolean containsProxy(Object entity) {
return proxiesByKey.containsValue( entity );
}
|
public int decrementCascadeLevel() {
return --cascading;
}
|
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;
}
|
public BatchFetchQueue getBatchFetchQueue() {
if (batchFetchQueue==null) {
batchFetchQueue = new BatchFetchQueue(this);
}
return batchFetchQueue;
}
Get the BatchFetchQueue, instantiating one if
necessary. |
public Object[] getCachedDatabaseSnapshot(EntityKey key) {
Object snapshot = entitySnapshotsByKey.get( key );
if ( snapshot == NO_ROW ) {
throw new IllegalStateException( "persistence context reported no row snapshot for " + MessageHelper.infoString( key.getEntityName(), key.getIdentifier() ) );
}
return ( Object[] ) snapshot;
}
Retrieve the cached database snapshot for the requested entity key.
This differs from #getDatabaseSnapshot is two important respects:
- no snapshot is obtained from the database if not already cached
- an entry of #NO_ROW here is interpretet as an exception
|
public int getCascadeLevel() {
return cascading;
}
Do we already know that the entity does not exist in the
database? |
public PersistentCollection getCollection(CollectionKey collectionKey) {
return (PersistentCollection) collectionsByKey.get(collectionKey);
}
Get the collection instance associated with the CollectionKey |
public Map getCollectionEntries() {
return collectionEntries;
}
|
public CollectionEntry getCollectionEntry(PersistentCollection coll) {
return (CollectionEntry) collectionEntries.get(coll);
}
Get the collection entry for a persistent collection |
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 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 PersistentCollection getCollectionHolder(Object array) {
return (PersistentCollection) arrayHolders.get(array);
}
Get the PersistentCollection object for an array |
public Object getCollectionOwner(Serializable key,
CollectionPersister collectionPersister) throws MappingException {
return getEntity( new EntityKey( key, collectionPersister.getOwnerEntityPersister(), session.getEntityMode() ) );
}
Get the entity that owns this persistent collection |
public Map getCollectionsByKey() {
return collectionsByKey;
}
|
public Object[] getDatabaseSnapshot(Serializable id,
EntityPersister persister) throws HibernateException {
EntityKey key = new EntityKey( id, persister, session.getEntityMode() );
Object cached = entitySnapshotsByKey.get(key);
if (cached!=null) {
return cached==NO_ROW ? null : (Object[]) cached;
}
else {
Object[] snapshot = persister.getDatabaseSnapshot( id, session );
entitySnapshotsByKey.put( key, snapshot==null ? NO_ROW : snapshot );
return snapshot;
}
}
Get the current state of the entity as known to the underlying
database, or null if there is no corresponding row |
public Map getEntitiesByKey() {
return entitiesByKey;
}
|
public Object getEntity(EntityKey key) {
return entitiesByKey.get(key);
}
Get the entity instance associated with the given
EntityKey |
public Object getEntity(EntityUniqueKey euk) {
return entitiesByUniqueKey.get(euk);
}
Get an entity cached by unique key |
public Map getEntityEntries() {
return entityEntries;
}
|
public EntityEntry getEntry(Object entity) {
return (EntityEntry) entityEntries.get(entity);
}
Retreive the EntityEntry representation of the given entity. |
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;
}
Search the persistence context for an index of the child object,
given a collection role |
public LoadContexts getLoadContexts() {
if ( loadContexts == null ) {
loadContexts = new LoadContexts( this );
}
return loadContexts;
}
|
public Serializable getLoadedCollectionOwnerIdOrNull(PersistentCollection collection) {
return getLoadedCollectionOwnerIdOrNull( getCollectionEntry( collection ) );
}
Get the ID for the entity that owned this persistent collection when it was loaded |
public Object getLoadedCollectionOwnerOrNull(PersistentCollection collection) {
CollectionEntry ce = getCollectionEntry( collection );
if ( ce.getLoadedPersister() == null ) {
return null; // early exit...
}
Object loadedOwner = null;
// TODO: an alternative is to check if the owner has changed; if it hasn't then
// return collection.getOwner()
Serializable entityId = getLoadedCollectionOwnerIdOrNull( ce );
if ( entityId != null ) {
loadedOwner = getCollectionOwner( entityId, ce.getLoadedPersister() );
}
return loadedOwner;
}
Get the entity that owned this persistent collection when it was loaded |
public Object[] getNaturalIdSnapshot(Serializable id,
EntityPersister persister) throws HibernateException {
if ( !persister.hasNaturalIdentifier() ) {
return null;
}
// if the natural-id is marked as non-mutable, it is not retrieved during a
// normal database-snapshot operation...
int[] props = persister.getNaturalIdentifierProperties();
boolean[] updateable = persister.getPropertyUpdateability();
boolean allNatualIdPropsAreUpdateable = true;
for ( int i = 0; i < props.length; i++ ) {
if ( !updateable[ props[i] ] ) {
allNatualIdPropsAreUpdateable = false;
break;
}
}
if ( allNatualIdPropsAreUpdateable ) {
// do this when all the properties are updateable since there is
// a certain likelihood that the information will already be
// snapshot-cached.
Object[] entitySnapshot = getDatabaseSnapshot( id, persister );
if ( entitySnapshot == NO_ROW ) {
return null;
}
Object[] naturalIdSnapshot = new Object[ props.length ];
for ( int i = 0; i < props.length; i++ ) {
naturalIdSnapshot[i] = entitySnapshot[ props[i] ];
}
return naturalIdSnapshot;
}
else {
return persister.getNaturalIdentifierSnapshot( id, session );
}
}
|
public HashSet getNullifiableEntityKeys() {
return nullifiableEntityKeys;
}
Retrieve the set of EntityKeys representing nullifiable references |
public Serializable getOwnerId(String entityName,
String propertyName,
Object childEntity,
Map mergeMap) {
final String collectionRole = entityName + '.' + propertyName;
final EntityPersister persister = session.getFactory().getEntityPersister( entityName );
final CollectionPersister collectionPersister = session.getFactory().getCollectionPersister( collectionRole );
// iterate all the entities currently associated with the persistence context.
Iterator entities = entityEntries.entrySet().iterator();
while ( entities.hasNext() ) {
final Map.Entry me = ( Map.Entry ) entities.next();
final EntityEntry entityEntry = ( EntityEntry ) me.getValue();
// does this entity entry pertain to the entity persister in which we are interested (owner)?
if ( persister.isSubclassEntityName( entityEntry.getEntityName() ) ) {
final Object entityEntryInstance = me.getKey();
//check if the managed object is the parent
boolean found = isFoundInParent(
propertyName,
childEntity,
persister,
collectionPersister,
entityEntryInstance
);
if ( !found && mergeMap != null ) {
//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;
}
Search this persistence context for an associated entity instance which is considered the "owner" of
the given childEntity, and return that owner's id value. This is performed in the scenario of a
uni-directional, non-inverse one-to-many collection (which means that the collection elements do not maintain
a direct reference to the owner).
As such, the processing here is basically to loop over every entity currently associated with this persistence
context and for those of the correct entity (sub) type to extract its collection role property value and see
if the child is contained within that collection. If so, we have found the owner; if not, we go on.
Also need to account for mergeMap which acts as a local copy cache managed for the duration of a merge
operation. It represents a map of the detached entity instances pointing to the corresponding managed instance. |
public Object getProxy(EntityKey key) {
return proxiesByKey.get(key);
}
Get an existing proxy by key |
public SessionImplementor getSession() {
return session;
}
|
public Serializable getSnapshot(PersistentCollection coll) {
return getCollectionEntry(coll).getSnapshot();
}
Get the snapshot of the pre-flush collection state |
public boolean hasNonReadOnlyEntities() {
return hasNonReadOnlyEntities;
}
|
public int incrementCascadeLevel() {
return ++cascading;
}
|
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();
}
}
}
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 boolean isEntryFor(Object entity) {
return entityEntries.containsKey(entity);
}
Is there an EntityEntry for this instance? |
public boolean isFlushing() {
return flushing;
}
|
public boolean isPropertyNull(EntityKey ownerKey,
String propertyName) {
return nullAssociations.contains( new AssociationKey(ownerKey, propertyName) );
}
Is the association property belonging to the keyed entity null? |
public boolean isStateless() {
return false;
}
|
public Object narrowProxy(Object proxy,
EntityPersister persister,
EntityKey key,
Object object) throws HibernateException {
boolean alreadyNarrow = persister.getConcreteProxyClass( session.getEntityMode() )
.isAssignableFrom( proxy.getClass() );
if ( !alreadyNarrow ) {
if ( PROXY_WARN_LOG.isWarnEnabled() ) {
PROXY_WARN_LOG.warn(
"Narrowing proxy to " +
persister.getConcreteProxyClass( session.getEntityMode() ) +
" - this operation breaks =="
);
}
if ( object != null ) {
proxiesByKey.remove(key);
return object; //return the proxied object
}
else {
proxy = persister.createProxy( key.getIdentifier(), session );
proxiesByKey.put(key, proxy); //overwrite old proxy
return proxy;
}
}
else {
if ( object != null ) {
LazyInitializer li = ( (HibernateProxy) proxy ).getHibernateLazyInitializer();
li.setImplementation(object);
}
return proxy;
}
}
If the existing proxy is insufficiently "narrow" (derived), instantiate a new proxy
and overwrite the registration of the old one. This breaks == and occurs only for
"class" proxies rather than "interface" proxies. Also init the proxy to point to
the given target implementation if necessary. |
public Object proxyFor(Object impl) throws HibernateException {
EntityEntry e = getEntry(impl);
EntityPersister p = e.getPersister();
return proxyFor( p, new EntityKey( e.getId(), p, session.getEntityMode() ), impl );
}
Return the existing proxy associated with the given EntityKey, or the
argument (the entity associated with the key) if no proxy exists.
(slower than the form above) |
public Object proxyFor(EntityPersister persister,
EntityKey key,
Object impl) throws HibernateException {
if ( !persister.hasProxy() ) return impl;
Object proxy = proxiesByKey.get(key);
if ( proxy != null ) {
return narrowProxy(proxy, persister, key, impl);
}
else {
return impl;
}
}
Return the existing proxy associated with the given EntityKey, or the
third argument (the entity associated with the key) if no proxy exists. Init
the proxy to the target implementation, if necessary. |
public boolean reassociateIfUninitializedProxy(Object value) throws MappingException {
if ( value instanceof ElementWrapper ) {
value = ( (ElementWrapper) value ).getElement();
}
if ( !Hibernate.isInitialized(value) ) {
HibernateProxy proxy = (HibernateProxy) value;
LazyInitializer li = proxy.getHibernateLazyInitializer();
reassociateProxy(li, proxy);
return true;
}
else {
return false;
}
}
Takes the given object and, if it represents a proxy, reassociates it with this event source. |
public void reassociateProxy(Object value,
Serializable id) throws MappingException {
if ( value instanceof ElementWrapper ) {
value = ( (ElementWrapper) value ).getElement();
}
if ( value instanceof HibernateProxy ) {
if ( log.isDebugEnabled() ) log.debug("setting proxy identifier: " + id);
HibernateProxy proxy = (HibernateProxy) value;
LazyInitializer li = proxy.getHibernateLazyInitializer();
li.setIdentifier(id);
reassociateProxy(li, proxy);
}
}
If a deleted entity instance is re-saved, and it has a proxy, we need to
reset the identifier of the proxy |
public PersistentCollection removeCollectionHolder(Object array) {
return (PersistentCollection) arrayHolders.remove(array);
}
|
public Object removeEntity(EntityKey key) {
Object entity = entitiesByKey.remove(key);
Iterator iter = entitiesByUniqueKey.values().iterator();
while ( iter.hasNext() ) {
if ( iter.next()==entity ) iter.remove();
}
entitySnapshotsByKey.remove(key);
nullifiableEntityKeys.remove(key);
getBatchFetchQueue().removeBatchLoadableEntityKey(key);
getBatchFetchQueue().removeSubselect(key);
return entity;
}
Remove an entity from the session cache, also clear
up other state associated with the entity, all except
for the EntityEntry |
public EntityEntry removeEntry(Object entity) {
return (EntityEntry) entityEntries.remove(entity);
}
Remove an entity entry from the session cache |
public Object removeProxy(EntityKey key) {
if ( batchFetchQueue != null ) {
batchFetchQueue.removeBatchLoadableEntityKey( key );
batchFetchQueue.removeSubselect( key );
}
return proxiesByKey.remove( key );
}
Remove a proxy from the session cache.
Additionally, ensure that any load optimization references
such as batch or subselect loading get cleaned up as well. |
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()
);
}
|
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 );
}
}
Used by the owning session to explicitly control serialization of the
persistence context. |
public void setEntryStatus(EntityEntry entry,
Status status) {
entry.setStatus(status);
setHasNonReadOnlyEnties(status);
}
|
public void setFlushing(boolean flushing) {
this.flushing = flushing;
}
|
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 String toString() {
return new StringBuffer()
.append("PersistenceContext[entityKeys=")
.append(entitiesByKey.keySet())
.append(",collectionKeys=")
.append(collectionsByKey.keySet())
.append("]")
.toString();
}
Returns a string representation of the object. |
public Object unproxy(Object maybeProxy) throws HibernateException {
if ( maybeProxy instanceof ElementWrapper ) {
maybeProxy = ( (ElementWrapper) maybeProxy ).getElement();
}
if ( maybeProxy instanceof HibernateProxy ) {
HibernateProxy proxy = (HibernateProxy) maybeProxy;
LazyInitializer li = proxy.getHibernateLazyInitializer();
if ( li.isUninitialized() ) {
throw new PersistentObjectException(
"object was an uninitialized proxy for " +
li.getEntityName()
);
}
return li.getImplementation(); //unwrap the object
}
else {
return maybeProxy;
}
}
Get the entity instance underlying the given proxy, throwing
an exception if the proxy is uninitialized. If the given object
is not a proxy, simply return the argument. |
public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException {
if ( maybeProxy instanceof ElementWrapper ) {
maybeProxy = ( (ElementWrapper) maybeProxy ).getElement();
}
if ( maybeProxy instanceof HibernateProxy ) {
HibernateProxy proxy = (HibernateProxy) maybeProxy;
LazyInitializer li = proxy.getHibernateLazyInitializer();
reassociateProxy(li, proxy);
return li.getImplementation(); //initialize + unwrap the object
}
else {
return maybeProxy;
}
}
Possibly unproxy the given reference and reassociate it with the current session. |
public PersistentCollection useUnownedCollection(CollectionKey key) {
if (unownedCollections==null) {
return null;
}
else {
return (PersistentCollection) unownedCollections.remove(key);
}
}
|