org.hibernate.engine.spi.EntityKey Java Examples

The following examples show how to use org.hibernate.engine.spi.EntityKey. 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: EntityReferenceInitializerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void handleMissingIdentifier(ResultSetProcessingContext context) {
	if ( EntityFetch.class.isInstance( entityReference ) ) {
		final EntityFetch fetch = (EntityFetch) entityReference;
		final EntityType fetchedType = fetch.getFetchedType();
		if ( ! fetchedType.isOneToOne() ) {
			return;
		}

		final EntityReferenceProcessingState fetchOwnerState = context.getOwnerProcessingState( fetch );
		if ( fetchOwnerState == null ) {
			throw new IllegalStateException( "Could not locate fetch owner state" );
		}

		final EntityKey ownerEntityKey = fetchOwnerState.getEntityKey();
		if ( ownerEntityKey != null ) {
			context.getSession().getPersistenceContext().addNullProperty(
					ownerEntityKey,
					fetchedType.getPropertyName()
			);
		}
	}
}
 
Example #2
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Attempts to load the entity from the second-level cache.
 *
 * @param event The load event
 * @param persister The persister for the entity being requested for load
 *
 * @return The entity from the second-level cache, or null.
 */
private Object loadFromSecondLevelCache(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey entityKey) {

	final SessionImplementor source = event.getSession();
	final boolean useCache = persister.canReadFromCache()
			&& source.getCacheMode().isGetEnabled()
			&& event.getLockMode().lessThan( LockMode.READ );

	if ( !useCache ) {
		// we can't use cache here
		return null;
	}

	final Object ce = getFromSharedCache( event, persister, source );

	if ( ce == null ) {
		// nothing was found in cache
		return null;
	}

	return processCachedEntry( event, persister, ce, source, entityKey );
}
 
Example #3
Source File: ReactivePersistenceContextAdapter.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
public CompletionStage<Object[]> reactiveGetDatabaseSnapshot(Serializable id, EntityPersister persister)
		throws HibernateException {

	SessionImplementor session = (SessionImplementor) getSession();
	final EntityKey key = session.generateEntityKey( id, persister );
	final Object[] cached = entitySnapshotsByKey == null ? null : entitySnapshotsByKey.get( key );
	if ( cached != null ) {
		return CompletionStages.completedFuture( cached == NO_ROW ? null : cached );
	}
	else {
		return ((ReactiveEntityPersister) persister).reactiveGetDatabaseSnapshot( id, session )
				.thenApply( snapshot -> {
					if ( entitySnapshotsByKey == null ) {
						entitySnapshotsByKey = new HashMap<>(8);
					}
					entitySnapshotsByKey.put( key, snapshot == null ? NO_ROW : snapshot );
					return snapshot;
				} );
	}
}
 
Example #4
Source File: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Performs the load of an entity.
 *
 * @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
 *
 * @return The loaded entity.
 */
private CompletionStage<Object> load( LoadEvent event, EntityPersister persister, EntityKey keyToLoad, LoadType options) {
	final EventSource session = event.getSession();
	if ( event.getInstanceToLoad() != null ) {
		if ( session.getPersistenceContextInternal().getEntry( event.getInstanceToLoad() ) != null ) {
			throw new PersistentObjectException(
					"attempted to load into an instance that was already associated with the session: " +
							MessageHelper.infoString( persister, event.getEntityId(), session.getFactory() ) );
		}
		persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), session );
	}

	return doLoad( event, persister, keyToLoad, options )
			.thenApply( optional -> {
				boolean isOptionalInstance = event.getInstanceToLoad() != null;
				if ( optional==null && ( !options.isAllowNulls() || isOptionalInstance ) ) {
					throwEntityNotFound( session, event.getEntityClassName(), event.getEntityId() );
				}
				else if ( isOptionalInstance && optional != event.getInstanceToLoad() ) {
					throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );
				}
				return optional;
			} );
}
 
Example #5
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void makeEntityCircularReferenceSafe(ReferenceCacheEntryImpl referenceCacheEntry,
											EventSource session,
											Object entity,
											EntityKey entityKey) {

	// make it circular-reference safe
	final StatefulPersistenceContext statefulPersistenceContext = (StatefulPersistenceContext) session.getPersistenceContext();

	if ( (entity instanceof ManagedEntity) ) {
		statefulPersistenceContext.addReferenceEntry(
				entity,
				Status.READ_ONLY
		);
	}
	else {
		TwoPhaseLoad.addUninitializedCachedEntity(
				entityKey,
				entity,
				referenceCacheEntry.getSubclassPersister(),
				LockMode.NONE,
				referenceCacheEntry.getVersion(),
				session
		);
	}
	statefulPersistenceContext.initializeNonLazyCollections();
}
 
Example #6
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void loadByDerivedIdentitySimplePkValue(
		LoadEvent event,
		LoadEventListener.LoadType options,
		EntityPersister dependentPersister,
		EmbeddedComponentType dependentIdType,
		EntityPersister parentPersister) {
	final EntityKey parentEntityKey = event.getSession().generateEntityKey( event.getEntityId(), parentPersister );
	final Object parent = doLoad( event, parentPersister, parentEntityKey, options );

	final Serializable dependent = (Serializable) dependentIdType.instantiate( parent, event.getSession() );
	dependentIdType.setPropertyValues( dependent, new Object[] {parent}, dependentPersister.getEntityMode() );
	final EntityKey dependentEntityKey = event.getSession().generateEntityKey( dependent, dependentPersister );
	event.setEntityId( dependent );

	event.setResult( doLoad( event, dependentPersister, dependentEntityKey, options ) );
}
 
Example #7
Source File: TwoPhaseLoad.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add an uninitialized instance of an entity class, as a placeholder to ensure object
 * identity. Must be called before <tt>postHydrate()</tt>.
 *
 * Create a "temporary" entry for a newly instantiated entity. The entity is uninitialized,
 * but we need the mapping from id to instance in order to guarantee uniqueness.
 *
 * @param key The entity key
 * @param object The entity instance
 * @param persister The entity persister
 * @param lockMode The lock mode
 * @param session The Session
 */
public static void addUninitializedEntity(
		final EntityKey key,
		final Object object,
		final EntityPersister persister,
		final LockMode lockMode,
		final SharedSessionContractImplementor session) {
	session.getPersistenceContext().addEntity(
			object,
			Status.LOADING,
			null,
			key,
			null,
			lockMode,
			true,
			persister,
			false
	);
}
 
Example #8
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
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) {
	addEntity( entityKey, entity );
	return addEntry(
			entity,
			status,
			loadedState,
			null,
			entityKey.getIdentifier(),
			version,
			lockMode,
			existsInDatabase,
			persister,
			disableVersionIncrement
	);
}
 
Example #9
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object removeEntity(EntityKey key) {
	final Object entity = entitiesByKey.remove( key );
	final Iterator itr = entitiesByUniqueKey.values().iterator();
	while ( itr.hasNext() ) {
		if ( itr.next() == entity ) {
			itr.remove();
		}
	}
	// Clear all parent cache
	parentsByChild.clear();
	entitySnapshotsByKey.remove( key );
	nullifiableEntityKeys.remove( key );
	if( batchFetchQueue != null ) {
		getBatchFetchQueue().removeBatchLoadableEntityKey(key);
		getBatchFetchQueue().removeSubselect(key);
	}
	return entity;
}
 
Example #10
Source File: AbstractLazyInitializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final void setReadOnly(boolean readOnly) {
	errorIfReadOnlySettingNotAvailable();
	// only update if readOnly is different from current setting
	if ( this.readOnly != readOnly ) {
		final EntityPersister persister = session.getFactory().getEntityPersister( entityName );
		if ( !persister.isMutable() && !readOnly ) {
			throw new IllegalStateException( "cannot make proxies [" + entityName + "#" + id + "] for immutable entities modifiable" );
		}
		this.readOnly = readOnly;
		if ( initialized ) {
			EntityKey key = generateEntityKeyOrNull( getIdentifier(), session, getEntityName() );
			if ( key != null && session.getPersistenceContext().containsEntity( key ) ) {
				session.getPersistenceContext().setReadOnly( target, readOnly );
			}
		}
	}
}
 
Example #11
Source File: SubselectOneToManyLoader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public SubselectOneToManyLoader(
		QueryableCollection persister, 
		String subquery,
		Collection entityKeys,
		QueryParameters queryParameters,
		Map<String, int[]> namedParameterLocMap,
		SessionFactoryImplementor factory, 
		LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
	super( persister, 1, subquery, factory, loadQueryInfluencers );

	keys = new Serializable[ entityKeys.size() ];
	Iterator iter = entityKeys.iterator();
	int i=0;
	while ( iter.hasNext() ) {
		keys[i++] = ( (EntityKey) iter.next() ).getIdentifier();
	}
	
	this.namedParameters = queryParameters.getNamedParameters();
	this.types = queryParameters.getFilteredPositionalParameterTypes();
	this.values = queryParameters.getFilteredPositionalParameterValues();
	this.namedParameterLocMap = namedParameterLocMap;
}
 
Example #12
Source File: SubselectCollectionLoader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public SubselectCollectionLoader(
		QueryableCollection persister, 
		String subquery,
		Collection entityKeys,
		QueryParameters queryParameters,
		Map<String, int[]> namedParameterLocMap,
		SessionFactoryImplementor factory, 
		LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
	super( persister, 1, subquery, factory, loadQueryInfluencers );

	keys = new Serializable[ entityKeys.size() ];
	Iterator iter = entityKeys.iterator();
	int i=0;
	while ( iter.hasNext() ) {
		keys[i++] = ( (EntityKey) iter.next() ).getIdentifier();
	}
	
	this.namedParameters = queryParameters.getNamedParameters();
	this.types = queryParameters.getFilteredPositionalParameterTypes();
	this.values = queryParameters.getFilteredPositionalParameterValues();
	this.namedParameterLocMap = namedParameterLocMap;
	
}
 
Example #13
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getEntityUsingInterceptor(EntityKey key) throws HibernateException {
	checkOpenOrWaitingForAutoClose();
	// todo : should this get moved to PersistentContext?
	// logically, is PersistentContext the "thing" to which an interceptor gets attached?
	final Object result = persistenceContext.getEntity( key );
	if ( result == null ) {
		final Object newObject = getInterceptor().getEntity( key.getEntityName(), key.getIdentifier() );
		if ( newObject != null ) {
			lock( newObject, LockMode.NONE );
		}
		return newObject;
	}
	else {
		return result;
	}
}
 
Example #14
Source File: ReactiveSubselectOneToManyLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
public ReactiveSubselectOneToManyLoader(
		QueryableCollection persister,
		String subquery,
		Collection entityKeys,
		QueryParameters queryParameters,
		Map<String, int[]> namedParameterLocMap,
		SessionFactoryImplementor factory,
		LoadQueryInfluencers loadQueryInfluencers) throws MappingException {
	super( persister, 1, subquery, factory, loadQueryInfluencers );

	keys = new Serializable[ entityKeys.size() ];
	Iterator iter = entityKeys.iterator();
	int i=0;
	while ( iter.hasNext() ) {
		keys[i++] = ( (EntityKey) iter.next() ).getIdentifier();
	}

	this.namedParameters = queryParameters.getNamedParameters();
	this.types = queryParameters.getFilteredPositionalParameterTypes();
	this.values = queryParameters.getFilteredPositionalParameterValues();
	this.namedParameterLocMap = namedParameterLocMap;
}
 
Example #15
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Object[] getDatabaseSnapshot(SessionImplementor session, EntityPersister persister, Serializable id) {
	if ( persister.isSelectBeforeUpdateRequired() ) {
		Object[] snapshot = session.getPersistenceContext()
				.getDatabaseSnapshot( id, persister );
		if ( snapshot == null ) {
			//do we even really need this? the update will fail anyway....
			if ( session.getFactory().getStatistics().isStatisticsEnabled() ) {
				session.getFactory().getStatistics()
						.optimisticFailure( persister.getEntityName() );
			}
			throw new StaleObjectStateException( persister.getEntityName(), id );
		}
		return snapshot;
	}
	// TODO: optimize away this lookup for entities w/o unsaved-value="undefined"
	final EntityKey entityKey = session.generateEntityKey( id, persister );
	return session.getPersistenceContext().getCachedDatabaseSnapshot( entityKey );
}
 
Example #16
Source File: EntityReturnReader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object read(ResultSet resultSet, ResultSetProcessingContext context) throws SQLException {
	final EntityReferenceProcessingState processingState = getIdentifierResolutionContext( context );

	final EntityKey entityKey = processingState.getEntityKey();
	final Object entityInstance = context.getProcessingState( entityReturn ).getEntityInstance();

	if ( context.shouldReturnProxies() ) {
		final Object proxy = context.getSession().getPersistenceContext().proxyFor(
				entityReturn.getEntityPersister(),
				entityKey,
				entityInstance
		);
		if ( proxy != entityInstance ) {
			( (HibernateProxy) proxy ).getHibernateLazyInitializer().setImplementation( proxy );
			return proxy;
		}
	}

	return entityInstance;
}
 
Example #17
Source File: EntityLoadQueryDetails.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object readRow(ResultSet resultSet, ResultSetProcessingContextImpl context) throws SQLException {
	final ResultSetProcessingContext.EntityReferenceProcessingState processingState =
			rootReturnReader.getIdentifierResolutionContext( context );
	// if the entity reference we are hydrating is a Return, it is possible that its EntityKey is
	// supplied by the QueryParameter optional entity information
	if ( context.shouldUseOptionalEntityInformation() && context.getQueryParameters().getOptionalId() != null ) {
		final EntityKey entityKey = context.getSession().generateEntityKey(
				context.getQueryParameters().getOptionalId(),
				processingState.getEntityReference().getEntityPersister()
		);
		processingState.registerIdentifierHydratedForm( entityKey.getIdentifier() );
		processingState.registerEntityKey( entityKey );
	}
	return super.readRow( resultSet, context );
}
 
Example #18
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 #19
Source File: Loader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The entity instance is already in the session cache
 */
private void instanceAlreadyLoaded(
		final ResultSet rs,
		final int i,
		final Loadable persister,
		final EntityKey key,
		final Object object,
		final LockMode requestedLockMode,
		final SharedSessionContractImplementor session)
		throws HibernateException, SQLException {
	if ( !persister.isInstance( object ) ) {
		throw new WrongClassException(
				"loaded object was of wrong class " + object.getClass(),
				key.getIdentifier(),
				persister.getEntityName()
		);
	}

	if ( LockMode.NONE != requestedLockMode && upgradeLocks() ) { //no point doing this if NONE was requested
		final EntityEntry entry = session.getPersistenceContext().getEntry( object );
		if ( entry.getLockMode().lessThan( requestedLockMode ) ) {
			//we only check the version when _upgrading_ lock modes
			if ( persister.isVersioned() ) {
				checkVersion( i, persister, key.getIdentifier(), object, rs, session );
			}
			//we need to upgrade the lock mode to the mode requested
			entry.setLockMode( requestedLockMode );
		}
	}
}
 
Example #20
Source File: DefaultMergeEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean existsInDatabase(Object entity, EventSource source, EntityPersister persister) {
	EntityEntry entry = source.getPersistenceContext().getEntry( entity );
	if ( entry == null ) {
		Serializable id = persister.getIdentifier( entity, source );
		if ( id != null ) {
			final EntityKey key = source.generateEntityKey( id, persister );
			final Object managedEntity = source.getPersistenceContext().getEntity( key );
			entry = source.getPersistenceContext().getEntry( managedEntity );
		}
	}

	return entry != null && entry.isExistsInDatabase();
}
 
Example #21
Source File: EntityReferenceInitializerImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void resolveEntityKey(ResultSet resultSet, ResultSetProcessingContextImpl context) {

	final EntityReferenceProcessingState processingState = context.getProcessingState( entityReference );

	// see if we already have an EntityKey associated with this EntityReference in the processing state.
	// if we do, this should have come from the optional entity identifier...
	final EntityKey entityKey = processingState.getEntityKey();
	if ( entityKey != null ) {
		log.debugf(
				"On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; " +
						"should only happen on root returns with an optional identifier specified"
		);
		return;
	}

	// Look for the hydrated form
	final Object identifierHydratedForm = processingState.getIdentifierHydratedForm();
	if ( identifierHydratedForm == null ) {
		// we need to register the missing identifier, but that happens later after all readers have had a chance
		// to resolve its EntityKey
		return;
	}

	final Type identifierType = entityReference.getEntityPersister().getIdentifierType();
	final Serializable resolvedId = (Serializable) identifierType.resolve(
			identifierHydratedForm,
			context.getSession(),
			null
	);
	if ( resolvedId != null ) {
		processingState.registerEntityKey(
				context.getSession().generateEntityKey( resolvedId, entityReference.getEntityPersister() )
		);
	}
}
 
Example #22
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the current state of the entity as known to the underlying
 * database, or null if there is no corresponding row
 * <p/>
 * {@inheritDoc}
 */
@Override
public Object[] getDatabaseSnapshot(Serializable id, EntityPersister persister) throws HibernateException {
	final EntityKey key = session.generateEntityKey( id, persister );
	final Object cached = entitySnapshotsByKey.get( key );
	if ( cached != null ) {
		return cached == NO_ROW ? null : (Object[]) cached;
	}
	else {
		final Object[] snapshot = persister.getDatabaseSnapshot( id, session );
		entitySnapshotsByKey.put( key, snapshot == null ? NO_ROW : snapshot );
		return snapshot;
	}
}
 
Example #23
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempts to locate the entity in the session-level cache.
 * <p/>
 * If allowed to return nulls, then if the entity happens to be found in
 * the session cache, we check the entity type for proper handling
 * of entity hierarchies.
 * <p/>
 * If checkDeleted was set to true, then if the entity is found in the
 * session-level cache, it's current status within the session cache
 * is checked to see if it has previously been scheduled for deletion.
 *
 * @param event The load event
 * @param keyToLoad The EntityKey representing the entity to be loaded.
 * @param options The load options.
 *
 * @return The entity from the session-level cache, or null.
 *
 * @throws HibernateException Generally indicates problems applying a lock-mode.
 */
protected Object loadFromSessionCache(
		final LoadEvent event,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options) throws HibernateException {

	SessionImplementor session = event.getSession();
	Object old = session.getEntityUsingInterceptor( keyToLoad );

	if ( old != null ) {
		// this object was already loaded
		EntityEntry oldEntry = session.getPersistenceContext().getEntry( old );
		if ( options.isCheckDeleted() ) {
			Status status = oldEntry.getStatus();
			if ( status == Status.DELETED || status == Status.GONE ) {
				return REMOVED_ENTITY_MARKER;
			}
		}
		if ( options.isAllowNulls() ) {
			final EntityPersister persister = event.getSession()
					.getFactory()
					.getEntityPersister( keyToLoad.getEntityName() );
			if ( !persister.isInstance( old ) ) {
				return INCONSISTENT_RTN_CLASS_MARKER;
			}
		}
		upgradeLock( old, oldEntry, event.getLockOptions(), event.getSession() );
	}

	return old;
}
 
Example #24
Source File: ReactivePersistenceContextAdapter.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Object[] getCachedDatabaseSnapshot(EntityKey key) {
	final Object[] snapshot = entitySnapshotsByKey == null ? null : entitySnapshotsByKey.get( key );
	if ( snapshot == NO_ROW ) {
		throw new IllegalStateException(
				"persistence context reported no row snapshot for "
						+ infoString( key.getEntityName(), key.getIdentifier() )
		);
	}
	return snapshot;
}
 
Example #25
Source File: BatchFetchQueueHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove the entity key with the specified {@code id} and {@code persister} from
 * the batch loadable entities {@link BatchFetchQueue}.
 *
 * @param id - the ID for the entity to be removed
 * @param persister - the entity persister
 * @param session - the session
 */
public static void removeBatchLoadableEntityKey(
		Serializable id,
		EntityPersister persister,
		SharedSessionContractImplementor session) {
	final EntityKey entityKey = session.generateEntityKey( id, persister );
	final BatchFetchQueue batchFetchQueue = session.getPersistenceContext().getBatchFetchQueue();
	batchFetchQueue.removeBatchLoadableEntityKey( entityKey );
}
 
Example #26
Source File: AbstractEntityTuplizer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setIdentifier(Object entity, Serializable id, EntityMode entityMode, SharedSessionContractImplementor session) {
	final Object[] extractedValues = mappedIdentifierType.getPropertyValues( id, entityMode );
	final Object[] injectionValues = new Object[extractedValues.length];
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	for ( int i = 0; i < virtualIdComponent.getSubtypes().length; i++ ) {
		final Type virtualPropertyType = virtualIdComponent.getSubtypes()[i];
		final Type idClassPropertyType = mappedIdentifierType.getSubtypes()[i];
		if ( virtualPropertyType.isEntityType() && !idClassPropertyType.isEntityType() ) {
			if ( session == null ) {
				throw new AssertionError(
						"Deprecated version of getIdentifier (no session) was used but session was required"
				);
			}
			final String associatedEntityName = ( (EntityType) virtualPropertyType ).getAssociatedEntityName();
			final EntityKey entityKey = session.generateEntityKey(
					(Serializable) extractedValues[i],
					sessionFactory.getMetamodel().entityPersister( associatedEntityName )
			);
			// it is conceivable there is a proxy, so check that first
			Object association = persistenceContext.getProxy( entityKey );
			if ( association == null ) {
				// otherwise look for an initialized version
				association = persistenceContext.getEntity( entityKey );
				if ( association == null ) {
					// get the association out of the entity itself
					association = sessionFactory.getMetamodel().entityPersister( entityName ).getPropertyValue(
							entity,
							virtualIdComponent.getPropertyNames()[i]
					);
				}
			}
			injectionValues[i] = association;
		}
		else {
			injectionValues[i] = extractedValues[i];
		}
	}
	virtualIdComponent.setPropertyValues( entity, injectionValues, entityMode );
}
 
Example #27
Source File: ResultSetProcessorHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static EntityKey getOptionalObjectKey(QueryParameters queryParameters, SharedSessionContractImplementor session) {
	final Object optionalObject = queryParameters.getOptionalObject();
	final Serializable optionalId = queryParameters.getOptionalId();
	final String optionalEntityName = queryParameters.getOptionalEntityName();

	return INSTANCE.interpretEntityKey( session, optionalEntityName, optionalId, optionalObject );
}
 
Example #28
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If the class to be loaded has been configured with a cache, then lock
 * given id in that cache and then perform the load.
 *
 * @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 source The originating session
 *
 * @return The loaded entity
 *
 * @throws HibernateException
 */
private Object lockAndLoad(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options,
		final SessionImplementor source) {
	SoftLock lock = null;
	final Object ck;
	final EntityDataAccess cache = persister.getCacheAccessStrategy();
	if ( persister.canWriteToCache() ) {
		ck = cache.generateCacheKey(
				event.getEntityId(),
				persister,
				source.getFactory(),
				source.getTenantIdentifier()
		);
		lock = persister.getCacheAccessStrategy().lockItem( source, ck, null );
	}
	else {
		ck = null;
	}

	Object entity;
	try {
		entity = load( event, persister, keyToLoad, options );
	}
	finally {
		if ( persister.canWriteToCache() ) {
			cache.unlockItem( source, ck, lock );
		}
	}

	return event.getSession().getPersistenceContext().proxyFor( persister, keyToLoad, entity );
}
 
Example #29
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Based on configured options, will either return a pre-existing proxy,
 * generate a new proxy, or perform an actual load.
 *
 * @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
 *
 * @return The result of the proxy/load operation.
 */
private Object proxyOrLoad(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options) {

	if ( traceEnabled ) {
		LOG.tracev(
				"Loading entity: {0}",
				MessageHelper.infoString( persister, event.getEntityId(), event.getSession().getFactory() )
		);
	}

	// this class has no proxies (so do a shortcut)
	if ( !persister.hasProxy() ) {
		return load( event, persister, keyToLoad, options );
	}

	final PersistenceContext persistenceContext = event.getSession().getPersistenceContext();

	// look for a proxy
	Object proxy = persistenceContext.getProxy( keyToLoad );
	if ( proxy != null ) {
		return returnNarrowedProxy( event, persister, keyToLoad, options, persistenceContext, proxy );
	}

	if ( options.isAllowProxyCreation() ) {
		return createProxyIfNecessary( event, persister, keyToLoad, options, persistenceContext );
	}

	// return a newly loaded object
	return load( event, persister, keyToLoad, options );
}
 
Example #30
Source File: Loader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void createSubselects(List keys, QueryParameters queryParameters, SharedSessionContractImplementor session) {
	if ( keys.size() > 1 ) { //if we only returned one entity, query by key is more efficient

		Set[] keySets = transpose( keys );

		Map namedParameterLocMap = buildNamedParameterLocMap( queryParameters );

		final Loadable[] loadables = getEntityPersisters();
		final String[] aliases = getAliases();
		final String subselectQueryString = SubselectFetch.createSubselectFetchQueryFragment( queryParameters );
		for ( Object key : keys ) {
			final EntityKey[] rowKeys = (EntityKey[]) key;
			for ( int i = 0; i < rowKeys.length; i++ ) {

				if ( rowKeys[i] != null && loadables[i].hasSubselectLoadableCollections() ) {

					SubselectFetch subselectFetch = new SubselectFetch(
							subselectQueryString,
							aliases[i],
							loadables[i],
							queryParameters,
							keySets[i],
							namedParameterLocMap
					);

					session.getPersistenceContext()
							.getBatchFetchQueue()
							.addSubselect( rowKeys[i], subselectFetch );
				}

			}

		}
	}
}