Code example for SessionImplementor

Methods: generateEntityKeygetFactory

0
	} 
 
	/** 
	 * 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, 
				persister.getEntityMode(), 
				session.getTenantIdentifier(), 
				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 ) { 
            LOG.debugf("Setting proxy identifier: %s", 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() ) { 
			final EntityPersister persister = session.getFactory().getEntityPersister( li.getEntityName() ); 
			final EntityKey key = session.generateEntityKey( li.getIdentifier(), persister ); 
		  	// 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 { 
 
		final Class concreteProxyClass = persister.getConcreteProxyClass(); 
		boolean alreadyNarrow = concreteProxyClass.isAssignableFrom( proxy.getClass() ); 
 
		if ( !alreadyNarrow ) { 
            if ( LOG.isEnabled(WARN) ) { 
				LOG.narrowingProxy( concreteProxyClass ); 
			} 
 
			if ( object != null ) { 
				proxiesByKey.remove(key); 
				return object; //return the proxied object 
			} 
			else { 
				proxy = persister.createProxy( key.getIdentifier(), session ); 
				Object proxyOrig = proxiesByKey.put(key, proxy); //overwrite old proxy 
				if ( proxyOrig != null ) { 
					if ( ! ( proxyOrig instanceof HibernateProxy ) ) { 
						throw new AssertionFailure( 
								"proxy not of type HibernateProxy; it is " + proxyOrig.getClass() 
						); 
					} 
					// set the read-only/modifiable mode in the new proxy to what it was in the original proxy 
					boolean readOnlyOrig = ( ( HibernateProxy ) proxyOrig ).getHibernateLazyInitializer().isReadOnly(); 
					( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().setReadOnly( readOnlyOrig ); 
				} 
				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); 
		return proxyFor( e.getPersister(), e.getEntityKey(), impl ); 
	} 
 
	/** 
	 * Get the entity that owns this persistent collection 
	 */ 
	public Object getCollectionOwner(Serializable key, CollectionPersister collectionPersister) throws MappingException { 
		return getEntity( session.generateEntityKey( key, collectionPersister.getOwnerEntityPersister() ) ); 
	} 
 
	/** 
	 * 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 ) { 
Stop searching for code, let great code find you!  Add Codota to your java IDE