diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/AbstractEntityEntry.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/AbstractEntityEntry.java index 6fc393284e97..5c2d48584593 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/AbstractEntityEntry.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/AbstractEntityEntry.java @@ -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; @@ -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 diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java index 8c50571e1fb1..af0cc0b811cd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java @@ -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 ) { diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 4378e56653a5..861baefa58e9 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -1263,9 +1263,12 @@ private static List 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 ) ); + } } } } @@ -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 ) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/CollectionType.java b/hibernate-core/src/main/java/org/hibernate/type/CollectionType.java index 34be9bee91fb..53e4c5f9b955 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/CollectionType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/CollectionType.java @@ -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 @@ -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