Skip to content

Commit 07bfe6a

Browse files
mbelladebeikov
authored andcommitted
HHH-17925 Allow mapping join column on single attribute of composite id
1 parent 8557c5c commit 07bfe6a

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

hibernate-core/src/main/java/org/hibernate/engine/internal/AbstractEntityEntry.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.hibernate.engine.spi.SessionImplementor;
2828
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2929
import org.hibernate.engine.spi.Status;
30+
import org.hibernate.metamodel.mapping.AttributeMapping;
3031
import org.hibernate.persister.entity.EntityPersister;
3132
import org.hibernate.type.TypeHelper;
3233

@@ -324,13 +325,16 @@ else if ( earlyInsert ) {
324325

325326
@Override
326327
public Object getLoadedValue(String propertyName) {
327-
return loadedState == null || propertyName == null
328-
? null
329-
: loadedState[ propertyIndex( propertyName ) ];
328+
if ( loadedState == null || propertyName == null ) {
329+
return null;
330+
}
331+
final int index = propertyIndex( propertyName );
332+
return index < 0 ? null : loadedState[index];
330333
}
331334

332335
private int propertyIndex(String propertyName) {
333-
return persister.findAttributeMapping( propertyName ).getStateArrayPosition();
336+
final AttributeMapping attributeMapping = persister.findAttributeMapping( propertyName );
337+
return attributeMapping != null ? attributeMapping.getStateArrayPosition() : -1;
334338
}
335339

336340
@Override

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ private static void interpretPluralAttributeMappingKeyDescriptor(
784784
fkTargetPart = collectionDescriptor.getOwnerEntityPersister().getIdentifierMapping();
785785
}
786786
else {
787-
fkTargetPart = declaringType.findContainingEntityMapping().findAttributeMapping( lhsPropertyName );
787+
fkTargetPart = declaringType.findContainingEntityMapping().findSubPart( lhsPropertyName );
788788
}
789789

790790
if ( keyType instanceof BasicType ) {

hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,9 +1270,12 @@ private static List<UniqueKeyEntry> initUniqueKeyEntries(final AbstractEntityPer
12701270
final AssociationType associationType = (AssociationType) propertyType;
12711271
final String ukName = associationType.getLHSPropertyName();
12721272
if ( ukName != null ) {
1273-
final int index = aep.findAttributeMapping( ukName ).getStateArrayPosition();
1274-
final Type type = aep.getPropertyTypes()[index];
1275-
uniqueKeys.add( new UniqueKeyEntry( ukName, index, type ) );
1273+
final AttributeMapping attributeMapping = aep.findAttributeMapping( ukName );
1274+
if ( attributeMapping != null ) {
1275+
final int index = attributeMapping.getStateArrayPosition();
1276+
final Type type = aep.getPropertyTypes()[index];
1277+
uniqueKeys.add( new UniqueKeyEntry( ukName, index, type ) );
1278+
}
12761279
}
12771280
}
12781281
}
@@ -4542,10 +4545,10 @@ public Object getPropertyValue(Object object, String propertyName) {
45424545
}
45434546
}
45444547
else if ( identifierMapping instanceof NonAggregatedIdentifierMapping ) {
4545-
final EmbeddedAttributeMapping embeddedAttributeMapping =
4546-
(EmbeddedAttributeMapping) findAttributeMapping( NavigableRole.IDENTIFIER_MAPPER_PROPERTY );
4547-
final AttributeMapping mapping = embeddedAttributeMapping == null ? null
4548-
: embeddedAttributeMapping.getMappedType().findAttributeMapping( basePropertyName );
4548+
final AttributeMapping mapping = ( (NonAggregatedIdentifierMapping) identifierMapping ).findSubPart(
4549+
propertyName,
4550+
null
4551+
).asAttributeMapping();
45494552
if ( mapping != null ) {
45504553
baseValue = mapping.getAttributeMetadata().getPropertyAccess().getGetter().get( object );
45514554
if ( dotIndex != -1 ) {

hibernate-core/src/main/java/org/hibernate/type/CollectionType.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public ForeignKeyDirection getForeignKeyDirection() {
369369
public Object getKeyOfOwner(Object owner, SharedSessionContractImplementor session) {
370370
final PersistenceContext pc = session.getPersistenceContextInternal();
371371

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

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

0 commit comments

Comments
 (0)