final GridType gridUniqueKeyType = getUniqueKeyTypeFromAssociatedEntity( propertyIndex, propertyName );
//get the associated property index (to get its column names)
//find the ids per unique property name
AssociationKeyMetadata associationKeyMetadata = inverseOneToOneAssociationKeyMetadata.get( propertyName );
if ( associationKeyMetadata == null ) {
throw new AssertionFailure( "loadByUniqueKey on a non EntityType:" + propertyName );
}
OgmEntityPersister inversePersister = (OgmEntityPersister) ((EntityType) getPropertyTypes()[propertyIndex]).getAssociatedJoinable( session.getFactory() );
OptionsServiceContext serviceContext = session.getFactory()
.getServiceRegistry()
.getService( OptionsService.class )
.context();
AssociationTypeContext associationTypeContext = new AssociationTypeContextImpl(
serviceContext.getPropertyOptions( inversePersister.getMappedClass(), associationKeyMetadata.getCollectionRole() ),
associationKeyMetadata.getAssociatedEntityKeyMetadata(),
getPropertyNames()[propertyIndex]
);
AssociationPersister associationPersister = new AssociationPersister(
inversePersister.getMappedClass()
)
.gridDialect( gridDialect )
.key( uniqueKey, gridUniqueKeyType )
.associationKeyMetadata( associationKeyMetadata )
.session( session )
.associationTypeContext( associationTypeContext )
.hostingEntity( session.getPersistenceContext().getEntity( new org.hibernate.engine.spi.EntityKey( (Serializable) uniqueKey, inversePersister ) ) );
final Association ids = associationPersister.getAssociationOrNull();
if (ids == null || ids.size() == 0 ) {
return null;
}
else if (ids.size() == 1) {
//EntityLoader#loadByUniqueKey uses a null object and LockMode.NONE
//there is only one element in the list, so get the first
Tuple tuple = ids.get( ids.getKeys().iterator().next() );
final Serializable id = (Serializable) getGridIdentifierType().nullSafeGet( tuple, getIdentifierColumnNames(), session, null );
return load( id, null, LockMode.NONE, session );
}
else {
throw new AssertionFailure(
"Loading by unique key but finding several matches: table:" + getTableName()
+ " property: " + propertyName
+ " value: " + uniqueKey );
}
}