Java Code Examples for javax.persistence.spi.LoadState#NOT_LOADED

The following examples show how to use javax.persistence.spi.LoadState#NOT_LOADED . 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: Persistence.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public boolean isLoaded(Object entity) {
    PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();

    List<PersistenceProvider> providers = resolver.getPersistenceProviders();

    for (PersistenceProvider provider : providers) {
        LoadState loadstate = provider.getProviderUtil().isLoaded(entity);
        if(loadstate == LoadState.LOADED) {
            return true;
        } else if (loadstate == LoadState.NOT_LOADED) {
            return false;
        } // else continue
    }
    //None of the providers could determine the load state
    return true;
}
 
Example 2
Source File: PersistenceUnitUtilImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean isLoaded(Object entity, String attributeName) {
	// added log message to help with HHH-7454, if state == LoadState,NOT_LOADED, returning true or false is not accurate.
	log.debug( "PersistenceUnitUtil#isLoaded is not always accurate; consider using EntityManager#contains instead" );
	LoadState state = PersistenceUtilHelper.isLoadedWithoutReference( entity, attributeName, cache );
	if ( state == LoadState.LOADED ) {
		return true;
	}
	else if ( state == LoadState.NOT_LOADED ) {
		return false;
	}
	else {
		return PersistenceUtilHelper.isLoadedWithReference(
				entity,
				attributeName,
				cache
		) != LoadState.NOT_LOADED;
	}
}
 
Example 3
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 4
Source File: PersistenceUnitUtilImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean isLoaded(Object entity) {
	// added log message to help with HHH-7454, if state == LoadState,NOT_LOADED, returning true or false is not accurate.
	log.debug( "PersistenceUnitUtil#isLoaded is not always accurate; consider using EntityManager#contains instead" );
	return PersistenceUtilHelper.isLoaded( entity ) != LoadState.NOT_LOADED;
}