Java Code Examples for org.hibernate.persister.entity.EntityPersister#createProxy()

The following examples show how to use org.hibernate.persister.entity.EntityPersister#createProxy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatelessSessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object internalLoad(
		String entityName,
		Serializable id,
		boolean eager,
		boolean nullable) throws HibernateException {
	checkOpen();
	EntityPersister persister = getFactory().getMetamodel().entityPersister( entityName );
	// first, try to load it from the temp PC associated to this SS
	Object loaded = temporaryPersistenceContext.getEntity( generateEntityKey( id, persister ) );
	if ( loaded != null ) {
		// we found it in the temp PC.  Should indicate we are in the midst of processing a result set
		// containing eager fetches via join fetch
		return loaded;
	}
	if ( !eager && persister.hasProxy() ) {
		// if the metadata allowed proxy creation and caller did not request forceful eager loading,
		// generate a proxy
		return persister.createProxy( id, this );
	}
	// otherwise immediately materialize it
	return get( entityName, id );
}
 
Example 2
Source File: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Object createProxy(
		LoadEvent event,
		EntityPersister persister,
		EntityKey keyToLoad,
		PersistenceContext persistenceContext) {
	// return new uninitialized proxy
	Object proxy = persister.createProxy( event.getEntityId(), event.getSession() );
	persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey( keyToLoad );
	persistenceContext.addProxy( keyToLoad, proxy );
	return proxy;
}
 
Example 3
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If there is already a corresponding proxy associated with the
 * persistence context, return it; otherwise create a proxy, associate it
 * with the persistence context, and return the just-created proxy.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 * @param persistenceContext The originating session
 *
 * @return The created/existing proxy
 */
private Object createProxyIfNecessary(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options,
		final PersistenceContext persistenceContext) {
	Object existing = persistenceContext.getEntity( keyToLoad );
	if ( existing != null ) {
		// return existing object or initialized proxy (unless deleted)
		if ( traceEnabled ) {
			LOG.trace( "Entity found in session cache" );
		}
		if ( options.isCheckDeleted() ) {
			EntityEntry entry = persistenceContext.getEntry( existing );
			Status status = entry.getStatus();
			if ( status == Status.DELETED || status == Status.GONE ) {
				return null;
			}
		}
		return existing;
	}
	if ( traceEnabled ) {
		LOG.trace( "Creating new proxy for entity" );
	}
	// return new uninitialized proxy
	Object proxy = persister.createProxy( event.getEntityId(), event.getSession() );
	persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey( keyToLoad );
	persistenceContext.addProxy( keyToLoad, proxy );
	return proxy;
}
 
Example 4
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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;
		
	}
	
}
 
Example 5
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Given that there is no pre-existing proxy.
 * Check if the entity is already loaded. If it is, return the entity,
 * otherwise create and return a proxy.
 */
private Object createProxyIfNecessary(
		final LoadEvent event, 
		final EntityPersister persister, 
		final EntityKey keyToLoad, 
		final LoadEventListener.LoadType options,
		final PersistenceContext persistenceContext
) {
	Object existing = persistenceContext.getEntity( keyToLoad );
	if ( existing != null ) {
		// return existing object or initialized proxy (unless deleted)
		log.trace( "entity found in session cache" );
		if ( options.isCheckDeleted() ) {
			EntityEntry entry = persistenceContext.getEntry( existing );
			Status status = entry.getStatus();
			if ( status == Status.DELETED || status == Status.GONE ) {
				return null;
			}
		}
		return existing;
	}
	else {
		log.trace( "creating new proxy for entity" );
		// return new uninitialized proxy
		Object proxy = persister.createProxy( event.getEntityId(), event.getSession() );
		persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey(keyToLoad);
		persistenceContext.addProxy(keyToLoad, proxy);
		return proxy;
	}
}
 
Example 6
Source File: StatelessSessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object internalLoad(
		String entityName,
        Serializable id,
        boolean eager,
        boolean nullable) throws HibernateException {
	errorIfClosed();
	EntityPersister persister = getFactory().getEntityPersister(entityName);
	if ( !eager && persister.hasProxy() ) {
		return persister.createProxy(id, this);
	}
	Object loaded = temporaryPersistenceContext.getEntity( new EntityKey(id, persister, EntityMode.POJO) );
	//TODO: if not loaded, throw an exception
	return loaded==null ? get( entityName, id ) : loaded;
}
 
Example 7
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key, Object object)
		throws HibernateException {

	final Class concreteProxyClass = persister.getConcreteProxyClass();
	final boolean alreadyNarrow = concreteProxyClass.isInstance( proxy );

	if ( !alreadyNarrow ) {
		LOG.narrowingProxy( concreteProxyClass );

		// If an impl is passed, there is really no point in creating a proxy.
		// It would just be extra processing.  Just return the impl
		if ( object != null ) {
			proxiesByKey.remove( key );
			return object;
		}

		// Similarly, if the original HibernateProxy is initialized, there
		// is again no point in creating a proxy.  Just return the impl
		final HibernateProxy originalHibernateProxy = (HibernateProxy) proxy;
		if ( !originalHibernateProxy.getHibernateLazyInitializer().isUninitialized() ) {
			final Object impl = originalHibernateProxy.getHibernateLazyInitializer().getImplementation();
			// can we return it?
			if ( concreteProxyClass.isInstance( impl ) ) {
				proxiesByKey.remove( key );
				return impl;
			}
		}


		// Otherwise, create the narrowed proxy
		final HibernateProxy narrowedProxy = (HibernateProxy) persister.createProxy( key.getIdentifier(), session );

		// set the read-only/modifiable mode in the new proxy to what it was in the original proxy
		final boolean readOnlyOrig = originalHibernateProxy.getHibernateLazyInitializer().isReadOnly();
		narrowedProxy.getHibernateLazyInitializer().setReadOnly( readOnlyOrig );

		return narrowedProxy;
	}
	else {

		if ( object != null ) {
			final LazyInitializer li = ( (HibernateProxy) proxy ).getHibernateLazyInitializer();
			li.setImplementation( object );
		}
		return proxy;
	}
}