Skip to content

Commit

Permalink
HHH-17925 Allow mapping join column on single attribute of composite id
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel committed May 28, 2024
1 parent 3652897 commit 6163259
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.Status;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.TypeHelper;

Expand Down Expand Up @@ -324,13 +325,16 @@ else if ( earlyInsert ) {

@Override
public Object getLoadedValue(String propertyName) {
return loadedState == null || propertyName == null
? null
: loadedState[ propertyIndex( propertyName ) ];
if ( loadedState == null || propertyName == null ) {
return null;
}
final int index = propertyIndex( propertyName );
return index < 0 ? null : loadedState[index];
}

private int propertyIndex(String propertyName) {
return persister.findAttributeMapping( propertyName ).getStateArrayPosition();
final AttributeMapping attributeMapping = persister.findAttributeMapping( propertyName );
return attributeMapping != null ? attributeMapping.getStateArrayPosition() : -1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ private static void interpretPluralAttributeMappingKeyDescriptor(
fkTargetPart = collectionDescriptor.getOwnerEntityPersister().getIdentifierMapping();
}
else {
fkTargetPart = declaringType.findContainingEntityMapping().findAttributeMapping( lhsPropertyName );
fkTargetPart = declaringType.findContainingEntityMapping().findSubPart( lhsPropertyName );
}

if ( keyType instanceof BasicType ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1263,9 +1263,12 @@ private static List<UniqueKeyEntry> initUniqueKeyEntries(final AbstractEntityPer
final AssociationType associationType = (AssociationType) propertyType;
final String ukName = associationType.getLHSPropertyName();
if ( ukName != null ) {
final int index = aep.findAttributeMapping( ukName ).getStateArrayPosition();
final Type type = aep.getPropertyTypes()[index];
uniqueKeys.add( new UniqueKeyEntry( ukName, index, type ) );
final AttributeMapping attributeMapping = aep.findAttributeMapping( ukName );
if ( attributeMapping != null ) {
final int index = attributeMapping.getStateArrayPosition();
final Type type = aep.getPropertyTypes()[index];
uniqueKeys.add( new UniqueKeyEntry( ukName, index, type ) );
}
}
}
}
Expand Down Expand Up @@ -4515,10 +4518,10 @@ public Object getPropertyValue(Object object, String propertyName) {
}
}
else if ( identifierMapping instanceof NonAggregatedIdentifierMapping ) {
final EmbeddedAttributeMapping embeddedAttributeMapping =
(EmbeddedAttributeMapping) findAttributeMapping( NavigableRole.IDENTIFIER_MAPPER_PROPERTY );
final AttributeMapping mapping = embeddedAttributeMapping == null ? null
: embeddedAttributeMapping.getMappedType().findAttributeMapping( basePropertyName );
final AttributeMapping mapping = ( (NonAggregatedIdentifierMapping) identifierMapping ).findSubPart(
propertyName,
null
).asAttributeMapping();
if ( mapping != null ) {
baseValue = mapping.getAttributeMetadata().getPropertyAccess().getGetter().get( object );
if ( dotIndex != -1 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public ForeignKeyDirection getForeignKeyDirection() {
public Object getKeyOfOwner(Object owner, SharedSessionContractImplementor session) {
final PersistenceContext pc = session.getPersistenceContextInternal();

EntityEntry entityEntry = pc.getEntry( owner );
final EntityEntry entityEntry = pc.getEntry( owner );
if ( entityEntry == null ) {
// This just handles a particular case of component
// projection, perhaps get rid of it and throw an exception
Expand All @@ -385,9 +385,10 @@ public Object getKeyOfOwner(Object owner, SharedSessionContractImplementor sessi
// later in the mapping document) - now, we could try and use e.getStatus()
// to decide to semiResolve(), trouble is that initializeEntity() reuses
// the same array for resolved and hydrated values
Object id = entityEntry.getLoadedState() != null
? entityEntry.getLoadedValue( foreignKeyPropertyName )
: entityEntry.getPersister().getPropertyValue( owner, foreignKeyPropertyName );
final Object loadedValue = entityEntry.getLoadedValue( foreignKeyPropertyName );
final Object id = loadedValue == null ?
entityEntry.getPersister().getPropertyValue( owner, foreignKeyPropertyName ) :
loadedValue;

// NOTE VERY HACKISH WORKAROUND!!
// TODO: Fix this so it will work for non-POJO entity mode
Expand Down

0 comments on commit 6163259

Please sign in to comment.