}
}
@Override
public void hydrateEntityState(ResultSet resultSet, ResultSetProcessingContextImpl context) {
final EntityReferenceProcessingState processingState = context.getProcessingState( entityReference );
// If there is no identifier for this entity reference for this row, nothing to do
if ( processingState.isMissingIdentifier() ) {
handleMissingIdentifier( context );
return;
}
// make sure we have the EntityKey
final EntityKey entityKey = processingState.getEntityKey();
if ( entityKey == null ) {
handleMissingIdentifier( context );
return;
}
// Have we already hydrated this entity's state?
if ( processingState.getEntityInstance() != null ) {
return;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// In getting here, we know that:
// 1) We need to hydrate the entity state
// 2) We have a valid EntityKey for the entity
// see if we have an existing entry in the session for this EntityKey
final Object existing = context.getSession().getEntityUsingInterceptor( entityKey );
if ( existing != null ) {
// It is previously associated with the Session, perform some checks
if ( ! entityReference.getEntityPersister().isInstance( existing ) ) {
throw new WrongClassException(
"loaded object was of wrong class " + existing.getClass(),
entityKey.getIdentifier(),
entityReference.getEntityPersister().getEntityName()
);
}
checkVersion( resultSet, context, entityKey, existing );
// use the existing association as the hydrated state
processingState.registerEntityInstance( existing );
//context.registerHydratedEntity( entityReference, entityKey, existing );
return;
}
// Otherwise, we need to load it from the ResultSet...
// determine which entity instance to use. Either the supplied one, or instantiate one
Object optionalEntityInstance = null;
if ( isReturn && context.shouldUseOptionalEntityInformation() ) {
final EntityKey optionalEntityKey = ResultSetProcessorHelper.getOptionalObjectKey(
context.getQueryParameters(),
context.getSession()
);
if ( optionalEntityKey != null ) {
if ( optionalEntityKey.equals( entityKey ) ) {
optionalEntityInstance = context.getQueryParameters().getOptionalObject();
}
}
}
final String concreteEntityTypeName = getConcreteEntityTypeName( resultSet, context, entityKey );
final Object entityInstance = optionalEntityInstance != null
? optionalEntityInstance
: context.getSession().instantiate( concreteEntityTypeName, entityKey.getIdentifier() );
processingState.registerEntityInstance( entityInstance );
// need to hydrate it.
// grab its state from the ResultSet and keep it in the Session
// (but don't yet initialize the object itself)
// note that we acquire LockMode.READ even if it was not requested