Java Code Examples for org.hibernate.proxy.LazyInitializer#getEntityName()

The following examples show how to use org.hibernate.proxy.LazyInitializer#getEntityName() . 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: 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 2
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 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: 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 5
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 6
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 );
}