org.hibernate.proxy.LazyInitializer Java Examples

The following examples show how to use org.hibernate.proxy.LazyInitializer. 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: Hibernate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check if the property is initialized. If the named property does not exist
 * or is not persistent, this method always returns <tt>true</tt>.
 *
 * @param proxy The potential proxy
 * @param propertyName the name of a persistent attribute of the object
 * @return true if the named property of the object is not listed as uninitialized; false otherwise
 */
public static boolean isPropertyInitialized(Object proxy, String propertyName) {
	final Object entity;
	if ( proxy instanceof HibernateProxy ) {
		final LazyInitializer li = ( (HibernateProxy) proxy ).getHibernateLazyInitializer();
		if ( li.isUninitialized() ) {
			return false;
		}
		else {
			entity = li.getImplementation();
		}
	}
	else {
		entity = proxy;
	}

	if ( entity instanceof PersistentAttributeInterceptable ) {
		PersistentAttributeInterceptor interceptor = ( (PersistentAttributeInterceptable) entity ).$$_hibernate_getInterceptor();
		if ( interceptor != null && interceptor instanceof LazyAttributeLoadingInterceptor ) {
			return ( (LazyAttributeLoadingInterceptor) interceptor ).isAttributeLoaded( propertyName );
		}
	}

	return true;
}
 
Example #2
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example #3
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example #4
Source File: CollectionType.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public boolean contains(Object collection, Object childObject, SharedSessionContractImplementor session) {
	// we do not have to worry about queued additions to uninitialized
	// collections, since they can only occur for inverse collections!
	Iterator elems = getElementsIterator( collection, session );
	while ( elems.hasNext() ) {
		Object element = elems.next();
		// worrying about proxies is perhaps a little bit of overkill here...
		if ( element instanceof HibernateProxy ) {
			LazyInitializer li = ( (HibernateProxy) element ).getHibernateLazyInitializer();
			if ( !li.isUninitialized() ) {
				element = li.getImplementation();
			}
		}
		if ( element == childObject ) {
			return true;
		}
	}
	return false;
}
 
Example #5
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example #6
Source File: Hibernate.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Check if the property is initialized. If the named property does not exist
 * or is not persistent, this method always returns <tt>true</tt>.
 *
 * @param proxy The potential proxy
 * @param propertyName the name of a persistent attribute of the object
 * @return true if the named property of the object is not listed as uninitialized
 * @return false if the object is an uninitialized proxy, or the named property is uninitialized
 */
public static boolean isPropertyInitialized(Object proxy, String propertyName) {
	
	Object entity;
	if ( proxy instanceof HibernateProxy ) {
		LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer();
		if ( li.isUninitialized() ) {
			return false;
		}
		else {
			entity = li.getImplementation();
		}
	}
	else {
		entity = proxy;
	}

	if ( FieldInterceptionHelper.isInstrumented( entity ) ) {
		FieldInterceptor interceptor = FieldInterceptionHelper.extractFieldInterceptor( entity );
		return interceptor == null || interceptor.isInitialized( propertyName );
	}
	else {
		return true;
	}
	
}
 
Example #7
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Given that there is a pre-existing proxy.
 * Initialize it if necessary; narrow if necessary.
 */
private Object returnNarrowedProxy(
		final LoadEvent event, 
		final EntityPersister persister, 
		final EntityKey keyToLoad, 
		final LoadEventListener.LoadType options, 
		final PersistenceContext persistenceContext, 
		final Object proxy
) {
	log.trace("entity proxy found in session cache");
	LazyInitializer li = ( (HibernateProxy) proxy ).getHibernateLazyInitializer();
	if ( li.isUnwrap() ) {
		return li.getImplementation();
	}
	Object impl = null;
	if ( !options.isAllowProxyCreation() ) {
		impl = load( event, persister, keyToLoad, options );
		if ( impl == null ) {
			event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( persister.getEntityName(), keyToLoad.getIdentifier());
		}
	}
	return persistenceContext.narrowProxy( proxy, persister, keyToLoad, impl );
}
 
Example #8
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String bestGuessEntityName(Object object) {
	if ( object instanceof HibernateProxy ) {
		LazyInitializer initializer = ( (HibernateProxy) object ).getHibernateLazyInitializer();
		// it is possible for this method to be called during flush processing,
		// so make certain that we do not accidentally initialize an uninitialized proxy
		if ( initializer.isUninitialized() ) {
			return initializer.getEntityName();
		}
		object = initializer.getImplementation();
	}
	EntityEntry entry = persistenceContext.getEntry( object );
	if ( entry == null ) {
		return guessEntityName( object );
	}
	else {
		return entry.getPersister().getEntityName();
	}
}
 
Example #9
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Serializable getIdentifier(Object object) throws HibernateException {
	checkOpen();
	checkTransactionSynchStatus();
	if ( object instanceof HibernateProxy ) {
		LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
		if ( li.getSession() != this ) {
			throw new TransientObjectException( "The proxy was not associated with this session" );
		}
		return li.getIdentifier();
	}
	else {
		EntityEntry entry = persistenceContext.getEntry( object );
		if ( entry == null ) {
			throw new TransientObjectException( "The instance was not associated with this session" );
		}
		return entry.getId();
	}
}
 
Example #10
Source File: SessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Serializable getIdentifier(Object object) throws HibernateException {
	errorIfClosed();
	checkTransactionSynchStatus();
	if ( object instanceof HibernateProxy ) {
		LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
		if ( li.getSession() != this ) {
			throw new TransientObjectException( "The proxy was not associated with this session" );
		}
		return li.getIdentifier();
	}
	else {
		EntityEntry entry = persistenceContext.getEntry(object);
		if ( entry == null ) {
			throw new TransientObjectException( "The instance was not associated with this session" );
		}
		return entry.getId();
	}
}
 
Example #11
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object unproxy(Object maybeProxy) throws HibernateException {
	if ( maybeProxy instanceof HibernateProxy ) {
		final HibernateProxy proxy = (HibernateProxy) maybeProxy;
		final LazyInitializer li = proxy.getHibernateLazyInitializer();
		if ( li.isUninitialized() ) {
			throw new PersistentObjectException(
					"object was an uninitialized proxy for " + li.getEntityName()
			);
		}
		//unwrap the object and return
		return li.getImplementation();
	}
	else {
		return maybeProxy;
	}
}
 
Example #12
Source File: SessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String bestGuessEntityName(Object object) {
	if (object instanceof HibernateProxy) {
		LazyInitializer initializer = ( ( HibernateProxy ) object ).getHibernateLazyInitializer();
		// it is possible for this method to be called during flush processing,
		// so make certain that we do not accidently initialize an uninitialized proxy
		if ( initializer.isUninitialized() ) {
			return initializer.getEntityName();
		}
		object = initializer.getImplementation();
	}
	EntityEntry entry = persistenceContext.getEntry(object);
	if (entry==null) {
		return guessEntityName(object);
	}
	else {
		return entry.getPersister().getEntityName();
	}
}
 
Example #13
Source File: PersistenceUtilHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Is the given attribute (by name) loaded?  This form must take care to not access the attribute (trigger
 * initialization).
 *
 * @param entity The entity
 * @param attributeName The name of the attribute to check
 * @param cache The cache we maintain of attribute resolutions
 *
 * @return The LoadState
 */
public static LoadState isLoadedWithReference(Object entity, String attributeName, MetadataCache cache) {
	if ( entity instanceof HibernateProxy ) {
		final LazyInitializer li = ( (HibernateProxy) entity ).getHibernateLazyInitializer();
		if ( li.isUninitialized() ) {
			// we have an uninitialized proxy, the attribute cannot be loaded
			return LoadState.NOT_LOADED;
		}
		else {
			// swap the proxy with target (for proper class name resolution)
			entity = li.getImplementation();
		}
	}

	try {
		final Class entityClass = entity.getClass();
		final Object attributeValue = cache.getClassMetadata( entityClass )
				.getAttributeAccess( attributeName )
				.extractValue( entity );
		return isLoaded( attributeValue );
	}
	catch (AttributeExtractionException ignore) {
		return LoadState.UNKNOWN;
	}
}
 
Example #14
Source File: SessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public boolean contains(Object object) {
	errorIfClosed();
	checkTransactionSynchStatus();
	if ( object instanceof HibernateProxy ) {
		//do not use proxiesByKey, since not all
		//proxies that point to this session's
		//instances are in that collection!
		LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
		if ( li.isUninitialized() ) {
			//if it is an uninitialized proxy, pointing
			//with this session, then when it is accessed,
			//the underlying instance will be "contained"
			return li.getSession()==this;
		}
		else {
			//if it is initialized, see if the underlying
			//instance is contained, since we need to 
			//account for the fact that it might have been
			//evicted
			object = li.getImplementation();
		}
	}
	// A session is considered to contain an entity only if the entity has
	// an entry in the session's persistence context and the entry reports
	// that the entity has not been removed
	EntityEntry entry = persistenceContext.getEntry( object );
	return entry != null && entry.getStatus() != Status.DELETED && entry.getStatus() != Status.GONE;
}
 
Example #15
Source File: FieldInterceptorImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object readObject(Object target, String name, Object oldValue) {
	Object value = intercept( target, name, oldValue );
	if (value instanceof HibernateProxy) {
		LazyInitializer li = ( (HibernateProxy) value ).getHibernateLazyInitializer();
		if ( li.isUnwrap() ) {
			value = li.getImplementation();
		}
	}
	return value;
}
 
Example #16
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void clear() {
	Iterator itr = proxiesByKey.values().iterator();
	while ( itr.hasNext() ) {
		final LazyInitializer li = ( ( HibernateProxy ) itr.next() ).getHibernateLazyInitializer();
		li.setSession( null );
	}
	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();
	}
}
 
Example #17
Source File: FieldInterceptorImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object readObject(Object target, String name, Object oldValue) {
	Object value = intercept( target, name, oldValue );
	if (value instanceof HibernateProxy) {
		LazyInitializer li = ( (HibernateProxy) value ).getHibernateLazyInitializer();
		if ( li.isUnwrap() ) {
			value = li.getImplementation();
		}
	}
	return value;
}
 
Example #18
Source File: CollectionType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public boolean contains(Object collection, Object childObject, SessionImplementor session) {
	// we do not have to worry about queued additions to uninitialized
	// collections, since they can only occur for inverse collections!
	Iterator elems = getElementsIterator( collection, session );
	while ( elems.hasNext() ) {
		Object element = elems.next();
		// worrying about proxies is perhaps a little bit of overkill here...
		if ( element instanceof HibernateProxy ) {
			LazyInitializer li = ( (HibernateProxy) element ).getHibernateLazyInitializer();
			if ( !li.isUninitialized() ) element = li.getImplementation();
		}
		if ( element == childObject ) return true;
	}
	return false;
}
 
Example #19
Source File: EntityType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Get the identifier value of an instance or proxy.
 * <p/>
 * Intended only for loggin purposes!!!
 *
 * @param object The object from which to extract the identifier.
 * @param persister The entity persister
 * @param entityMode The entity mode
 * @return The extracted identifier.
 */
private static Serializable getIdentifier(Object object, EntityPersister persister, EntityMode entityMode) {
	if (object instanceof HibernateProxy) {
		HibernateProxy proxy = (HibernateProxy) object;
		LazyInitializer li = proxy.getHibernateLazyInitializer();
		return li.getIdentifier();
	}
	else {
		return persister.getIdentifier( object, entityMode );
	}
}
 
Example #20
Source File: HibernateUtils.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * THIS CAN BE REMOVED WHEN SAKAI IS ON HIBERNATE >= 5.2.10
 *
 * Unproxies a {@link HibernateProxy}. If the proxy is uninitialized, it automatically triggers an initialization.
 * In case the supplied object is null or not a proxy, the object will be returned as-is.
 *
 * @param proxy the {@link HibernateProxy} to be unproxied
 * @return the proxy's underlying implementation object, or the supplied object otherwise
 */
public static Object unproxy(Object proxy) {
    if ( proxy instanceof HibernateProxy ) {
        HibernateProxy hibernateProxy = (HibernateProxy) proxy;
        LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();
        return initializer.getImplementation();
    } else {
        return proxy;
    }
}
 
Example #21
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 #22
Source File: HibernateUtils.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * THIS CAN BE REMOVED WHEN SAKAI IS ON HIBERNATE >= 5.2.10
 *
 * Unproxies a {@link HibernateProxy}. If the proxy is uninitialized, it automatically triggers an initialization.
 * In case the supplied object is null or not a proxy, the object will be returned as-is.
 *
 * @param proxy the {@link HibernateProxy} to be unproxied
 * @return the proxy's underlying implementation object, or the supplied object otherwise
 */
public static Object unproxy(Object proxy) {
    if ( proxy instanceof HibernateProxy ) {
        HibernateProxy hibernateProxy = (HibernateProxy) proxy;
        LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer();
        return initializer.getImplementation();
    } else {
        return proxy;
    }
}
 
Example #23
Source File: HibernateUnproxyObjectAccessHook.java    From javers with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<ObjectAccessProxy<T>> createAccessor(T entity) {
    if (entity instanceof HibernateProxy) {
        LazyInitializer lazyInitializer = ((HibernateProxy) entity).getHibernateLazyInitializer();

        return fromLazyInitializer(lazyInitializer);
    }
    if (entity instanceof JavassistLazyInitializer){
        JavassistLazyInitializer proxy = (JavassistLazyInitializer) entity;
        return fromLazyInitializer(proxy);
    }

    return Optional.empty();
}
 
Example #24
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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 );
	}
}
 
Example #25
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example #26
Source File: Models.java    From commafeed with Apache License 2.0 5 votes vote down vote up
/**
 * extract the id from the proxy without initializing it
 */
public static Long getId(AbstractModel model) {
	if (model instanceof HibernateProxy) {
		LazyInitializer lazyInitializer = ((HibernateProxy) model).getHibernateLazyInitializer();
		if (lazyInitializer.isUninitialized()) {
			return (Long) lazyInitializer.getIdentifier();
		}
	}
	return model.getId();
}
 
Example #27
Source File: ReactiveSessionImpl.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public <T> CompletionStage<T> reactiveFetch(T association, boolean unproxy) {
	checkOpen();
	if ( association instanceof HibernateProxy ) {
		LazyInitializer initializer = ((HibernateProxy) association).getHibernateLazyInitializer();
		//TODO: is this correct?
		// SessionImpl doesn't use IdentifierLoadAccessImpl for initializing proxies
		String entityName = initializer.getEntityName();
		Serializable identifier = initializer.getIdentifier();
		return new ReactiveIdentifierLoadAccessImpl<T>( entityName )
				.fetch( identifier )
				.thenApply( SessionUtil.checkEntityFound( this, entityName, identifier ) )
				.thenApply( entity -> {
					initializer.setSession( this );
					initializer.setImplementation( entity );
					return unproxy ? entity : association;
				} );
	}
	else if ( association instanceof PersistentCollection ) {
		PersistentCollection persistentCollection = (PersistentCollection) association;
		return reactiveInitializeCollection( persistentCollection, false )
				.thenApply( pc -> association );
	}
	else {
		return CompletionStages.completedFuture( association );
	}
}
 
Example #28
Source File: SimpleHibernateProxyHandler.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
public void initialize(Object o) {
    if (o instanceof HibernateProxy) {
        final LazyInitializer hibernateLazyInitializer = ((HibernateProxy)o).getHibernateLazyInitializer();
        if (hibernateLazyInitializer.isUninitialized()) {
            hibernateLazyInitializer.initialize();
        }
    }
    else {
        super.initialize(o);
    }
}
 
Example #29
Source File: SimpleHibernateProxyHandler.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
public Object unwrapProxy(final HibernateProxy proxy) {
    final LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
    if (lazyInitializer.isUninitialized()) {
        lazyInitializer.initialize();
    }
    final Object obj = lazyInitializer.getImplementation();
    if (obj != null) {
        ensureCorrectGroovyMetaClass(obj, obj.getClass());
    }
    return obj;
}
 
Example #30
Source File: AnyType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private EntityPersister guessEntityPersister(Object object) {
	if ( scope == null ) {
		return null;
	}

	String entityName = null;

	// this code is largely copied from Session's bestGuessEntityName
	Object entity = object;
	if ( entity instanceof HibernateProxy ) {
		final LazyInitializer initializer = ( (HibernateProxy) entity ).getHibernateLazyInitializer();
		if ( initializer.isUninitialized() ) {
			entityName = initializer.getEntityName();
		}
		entity = initializer.getImplementation();
	}

	if ( entityName == null ) {
		for ( EntityNameResolver resolver : scope.getTypeConfiguration().getSessionFactory().getMetamodel().getEntityNameResolvers() ) {
			entityName = resolver.resolveEntityName( entity );
			if ( entityName != null ) {
				break;
			}
		}
	}

	if ( entityName == null ) {
		// the old-time stand-by...
		entityName = object.getClass().getName();
	}

	return scope.getTypeConfiguration().getSessionFactory().getMetamodel().entityPersister( entityName );
}