📄 persistencecontext.java
字号:
/** * Add an entity to the cache by unique key */ public void addEntity(EntityUniqueKey euk, Object entity) { entitiesByUniqueKey.put(euk, entity); } /** * Retreive the EntityEntry representation of the given entity. * * @param object The entity for which to locate the EntityEntry. * @return The EntityEntry for the given entity. */ public EntityEntry getEntry(Object object) { return (EntityEntry) entityEntries.get(object); } /** * Remove an entity entry from the session cache */ public EntityEntry removeEntry(Object object) { return (EntityEntry) entityEntries.remove(object); } /** * Is there an EntityEntry for this instance? */ public boolean isEntryFor(Object object) { return entityEntries.containsKey(object); } /** * Get the collection entry for a persistent collection */ public CollectionEntry getCollectionEntry(PersistentCollection coll) { return (CollectionEntry) collectionEntries.get(coll); } /** * Adds an entity to the internal caches. */ public EntityEntry addEntity( final Object object, final Status status, final Object[] loadedState, final Serializable id, final Object version, final LockMode lockMode, final boolean existsInDatabase, final EntityPersister persister, final boolean disableVersionIncrement ) { addEntity( new EntityKey( id, persister, session.getEntityMode() ), object ); return addEntry(object, status, loadedState, null, id, version, lockMode, existsInDatabase, persister, disableVersionIncrement); } /** * Generates an appropriate EntityEntry instance and adds it * to the event source's internal caches. */ public EntityEntry addEntry( final Object object, final Status status, final Object[] loadedState, final Object rowId, final Serializable id, final Object version, final LockMode lockMode, final boolean existsInDatabase, final EntityPersister persister, final boolean disableVersionIncrement) { EntityEntry e = new EntityEntry( status, loadedState, rowId, id, version, lockMode, existsInDatabase, persister, session.getEntityMode(), disableVersionIncrement ); entityEntries.put(object, e); setHasNonReadOnlyEnties(status); return e; } public LockMode getLockMode(Object entity) { return getEntry(entity).getLockMode(); } public void setLockMode(Object entity, LockMode lockMode) { getEntry(entity).setLockMode(lockMode); } public boolean containsProxy(Object object) { return proxiesByKey.values().contains( object ); } /** * Takes the given object and, if it represents a proxy, reassociates it with this event source. * * @param value The possible proxy to be reassociated. * @return Whether the passed value represented an actual proxy which got initialized. * @throws MappingException */ public boolean reassociateIfUninitializedProxy(Object value) throws MappingException { if ( !Hibernate.isInitialized(value) ) { HibernateProxy proxy = (HibernateProxy) value; LazyInitializer li = proxy.getHibernateLazyInitializer(); reassociateProxy(li, proxy); return true; } else { return false; } } /** * If a deleted entity instance is re-saved, and it has a proxy, we need to * reset the identifier of the proxy */ public void reassociateProxy(Object value, Serializable id) throws MappingException { 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); } } /** * Associate a proxy that was instantiated by another session with this session */ public void reassociateProxy(LazyInitializer li, HibernateProxy proxy) throws HibernateException { if ( li.getSession() != this ) { EntityPersister persister = session.getFactory().getEntityPersister( li.getEntityName() ); EntityKey key = new EntityKey( li.getIdentifier(), persister, session.getEntityMode() ); if ( !proxiesByKey.containsKey(key) ) proxiesByKey.put(key, proxy); // any earlier proxy takes precedence proxy.getHibernateLazyInitializer().setSession(session); } } /** * 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 unproxy(Object maybeProxy) throws HibernateException { 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; } } /** * Possibly unproxy the given reference and reassociate it with the current session. * * @param maybeProxy The reference to be unproxied if it currently represents a proxy. * @return The unproxied instance. * @throws HibernateException */ public Object unproxyAndReassociate(Object maybeProxy) throws HibernateException { 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; } } /** * Attempts to check whether the given key represents an entity already loaded within the * current session. * @param id The key to be checked. * @param object The entity reference against which to perform the uniqueness check. * @throws HibernateException */ public void checkUniqueness(Serializable id, EntityPersister persister, Object object) throws HibernateException { Object entity = getEntity( new EntityKey( id, persister, session.getEntityMode() ) ); if ( entity == object ) { throw new AssertionFailure( "object already associated, but no entry was found" ); } if ( entity != null ) { throw new NonUniqueObjectException( id, persister.getEntityName() ); } } /** * 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. * * @param proxy The proxied object to be narrowed. * @param persister The persister for the proxied entity. * @param key The internal cache key for the proxied entity. * @param object (optional) the actual proxied entity instance. * @return An appropriately narrowed instance. * @throws HibernateException */ public Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key, Object object) throws HibernateException { if ( !persister.getConcreteProxyClass( session.getEntityMode() ).isAssignableFrom( proxy.getClass() ) ) { if ( log.isWarnEnabled() ) 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 { return proxy; } } /** * Return the existing proxy associated with the given <tt>EntityKey</tt>, or the * second argument (the entity associated with the key) if no proxy exists. */ 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 <tt>EntityKey</tt>, or the * second argument (the entity associated with the key) if no proxy exists. * (slower than the form above) */ 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 ); } /** * Get the entity that owns this persistent collection */ public Object getCollectionOwner(Serializable key, CollectionPersister collectionPersister) throws MappingException { return getEntity( new EntityKey( key, collectionPersister.getOwnerEntityPersister(), session.getEntityMode() ) ); } /** * add a collection we just loaded up (still needs initializing) */ public void addUninitializedCollection(PersistentCollection collection, CollectionPersister persister, Serializable id, EntityMode em) { CollectionEntry ce = new CollectionEntry(persister, id, flushing); collection.setCollectionSnapshot(ce); addCollection(collection, ce, id, em); } /** * add a detached uninitialized collection */ public void addUninitializedDetachedCollection(PersistentCollection collection, CollectionPersister persister, Serializable id, EntityMode em) { CollectionEntry ce = new CollectionEntry(persister, id); collection.setCollectionSnapshot(ce); addCollection(collection, ce, id, em); } /** * Add a new collection (ie. a newly created one, just instantiated by the * application, with no database state or snapshot) * * @param collection The collection to be associated with the persistence context */ public void addNewCollection(PersistentCollection collection, CollectionPersister persister) throws HibernateException { CollectionEntry ce = addCollection(collection); if ( persister.hasOrphanDelete() ) { ce.initSnapshot(collection, persister); } } /** * Add an collection to the cache, with a given collection entry */ private void addCollection(PersistentCollection coll, CollectionEntry entry, Serializable key, EntityMode em) { collectionEntries.put(coll, entry); PersistentCollection old = (PersistentCollection) collectionsByKey.put( new CollectionKey( entry.getLoadedPersister(), key, em ), coll ); if ( old != null ) { if (old==coll) throw new AssertionFailure("bug adding collection twice"); // or should it actually throw an exception? old.unsetSession(session); collectionEntries.remove(old); // watch out for a case where old is still referenced // somewhere in the object graph! (which is a user error) } } /** * Add a collection to the cache, creating a new collection entry for it */ private CollectionEntry addCollection(PersistentCollection collection) throws HibernateException { CollectionEntry ce = new CollectionEntry(); collectionEntries.put(collection, ce); collection.setCollectionSnapshot(ce); return ce; } /** * 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 addInitializedDetachedCollection(PersistentCollection collection, CollectionSnapshot cs, EntityMode em)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -