⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statefulpersistencecontext.java

📁 一个Java持久层类库
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	public EntityEntry getEntry(Object entity) {		return (EntityEntry) entityEntries.get(entity);	}	/**	 * Remove an entity entry from the session cache	 */	public EntityEntry removeEntry(Object entity) {		return (EntityEntry) entityEntries.remove(entity);	}	/**	 * Is there an EntityEntry for this instance?	 */	public boolean isEntryFor(Object entity) {		return entityEntries.containsKey(entity);	}	/**	 * 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 entity,			final Status status,			final Object[] loadedState,			final EntityKey entityKey,			final Object version,			final LockMode lockMode,			final boolean existsInDatabase,			final EntityPersister persister,			final boolean disableVersionIncrement, 			boolean lazyPropertiesAreUnfetched	) {				addEntity( entityKey, entity );				return addEntry(				entity,				status,				loadedState,				null,				entityKey.getIdentifier(),				version,				lockMode,				existsInDatabase,				persister,				disableVersionIncrement, 				lazyPropertiesAreUnfetched			);	}	/**	 * Generates an appropriate EntityEntry instance and adds it 	 * to the event source's internal caches.	 */	public EntityEntry addEntry(			final Object entity,			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, 			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;	}	public boolean containsCollection(PersistentCollection collection) {		return collectionEntries.containsKey(collection);	}	public boolean containsProxy(Object entity) {		return proxiesByKey.containsValue( entity );	}		/**	 * 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 ( 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;		}	}	/**	 * 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 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);		}	}	/**	 * Associate a proxy that was instantiated by another session with this session	 *	 * @param li The proxy initializer.	 * @param proxy The proxy to reassociate.	 */	private void reassociateProxy(LazyInitializer li, HibernateProxy proxy) {		if ( li.getSession() != this.getSession() ) {			EntityPersister persister = session.getFactory().getEntityPersister( li.getEntityName() );			EntityKey key = new EntityKey( li.getIdentifier(), persister, session.getEntityMode() );		  	// any earlier proxy takes precedence			if ( !proxiesByKey.containsKey( key ) ) {				proxiesByKey.put( key, proxy );			}			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 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;		}	}	/**	 * 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 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;		}	}	/**	 * Attempts to check whether the given key represents an entity already loaded within the	 * current session.	 * @param object The entity reference against which to perform the uniqueness check.	 * @throws HibernateException	 */	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() );		}	}	/**	 * 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.	 *	 * @param proxy The proxy instance 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 {				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;					}			}	/**	 * Return the existing proxy associated with the given <tt>EntityKey</tt>, or the	 * third argument (the entity associated with the key) if no proxy exists. Init	 * the proxy to the target implementation, if necessary.	 */	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	 * 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() ) );	}	/**	 * Get the entity that owned this persistent collection when it was loaded	 *	 * @param collection The persistent collection	 * @return the owner, if its entity ID is available from the collection's loaded key	 * and the owner entity is in the persistence context; otherwise, returns null	 */	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 ID for the entity that owned this persistent collection when it was loaded	 *	 * @param collection The persistent collection	 * @return the owner ID if available from the collection's loaded key; otherwise, returns null	 */	public Serializable getLoadedCollectionOwnerIdOrNull(PersistentCollection collection) {		return getLoadedCollectionOwnerIdOrNull( getCollectionEntry( collection ) );	}	/**	 * Get the ID for the entity that owned this persistent collection when it was loaded	 *	 * @param ce The collection entry	 * @return the owner ID if available from the collection's loaded key; otherwise, returns null	 */	private Serializable getLoadedCollectionOwnerIdOrNull(CollectionEntry ce) {		if ( ce == null || ce.getLoadedKey() == null || ce.getLoadedPersister() == null ) {			return null;		}		// TODO: an alternative is to check if the owner has changed; if it hasn't then		// get the ID from collection.getOwner()		return ce.getLoadedPersister().getCollectionType().getIdOfOwnerOrNull( ce.getLoadedKey(), session );	}	/**	 * add a collection we just loaded up (still needs initializing)	 */	public void addUninitializedCollection(CollectionPersister persister, PersistentCollection collection, Serializable id) {		CollectionEntry ce = new CollectionEntry(collection, persister, id, flushing);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -