Skip to content

Commit

Permalink
Use FK descriptor instead of target entity identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Mar 12, 2021
1 parent c582272 commit 06d2a0c
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 33 deletions.
Expand Up @@ -16,6 +16,7 @@
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.SelectionConsumer;
import org.hibernate.metamodel.mapping.SelectionMappings;
import org.hibernate.metamodel.mapping.CompositeIdentifierMapping;
Expand Down Expand Up @@ -202,12 +203,9 @@ public int forEachJdbcValue(
final AttributeMapping attributeMapping = attributeMappings.get( i );
final Object o = attributeMapping.getPropertyAccess().getGetter().get( value );
if ( attributeMapping instanceof ToOneAttributeMapping ) {
final EntityMappingType associatedEntityMappingType =
( (ToOneAttributeMapping) attributeMapping ).getAssociatedEntityMappingType();
final EntityIdentifierMapping identifierMapping =
associatedEntityMappingType.getIdentifierMapping();
final Object identifier = identifierMapping.getIdentifier( o, session );
span += identifierMapping.forEachJdbcValue(
final ForeignKeyDescriptor fkDescriptor = ( (ToOneAttributeMapping) attributeMapping ).getForeignKeyDescriptor();
final Object identifier = fkDescriptor.getAssociationKeyFromTarget( o, session );
span += fkDescriptor.forEachJdbcValue(
identifier,
clause,
span + offset,
Expand Down
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.metamodel.mapping;

import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.SqlAstJoinType;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
Expand Down Expand Up @@ -65,6 +66,8 @@ default int forEachSelection(int offset, SelectionConsumer consumer) {
return visitReferringColumns( offset, consumer );
}

Object getAssociationKeyFromTarget(Object targetObject, SharedSessionContractImplementor session);

int visitReferringColumns(int offset, SelectionConsumer consumer);

int visitTargetColumns(int offset, SelectionConsumer consumer);
Expand Down
Expand Up @@ -331,11 +331,7 @@ public void breakDownJdbcValues(Object domainValue, JdbcValueConsumer valueConsu
if ( attributeMapping instanceof ToOneAttributeMapping ) {
final ToOneAttributeMapping toOne = (ToOneAttributeMapping) attributeMapping;
final ForeignKeyDescriptor fKDescriptor = toOne.getForeignKeyDescriptor();

final EntityMappingType associatedEntityMapping = toOne.getEntityMappingType();
final EntityIdentifierMapping associatedEntityMappingIdentifierMapping = associatedEntityMapping.getIdentifierMapping();

final Object keyValue = value == null ? null : associatedEntityMappingIdentifierMapping.getIdentifier( value, session );
final Object keyValue = value == null ? null : fKDescriptor.disassemble( value, session );
fKDescriptor.breakDownJdbcValues( keyValue, valueConsumer, session );
}
else {
Expand Down
Expand Up @@ -409,6 +409,16 @@ public void breakDownJdbcValues(Object domainValue, JdbcValueConsumer valueConsu
);
}

@Override
public Object getAssociationKeyFromTarget(Object targetObject, SharedSessionContractImplementor session) {
// If the mapping type has an identifier type, that identifier is the key
if ( mappingType instanceof SingleAttributeIdentifierMapping ) {
return ( (SingleAttributeIdentifierMapping) mappingType ).getIdentifier( targetObject, session );
}
// Otherwise this is a key based on the target object i.e. without id-class
return targetObject;
}

@Override
public EntityMappingType findContainingEntityMapping() {
throw new UnsupportedOperationException();
Expand Down
Expand Up @@ -899,7 +899,8 @@ private static void interpretPluralAttributeMappingKeyDescriptor(
final Type keyType = bootValueMappingKey.getType();
final ModelPart fkTarget;
final String lhsPropertyName = collectionDescriptor.getCollectionType().getLHSPropertyName();
if ( lhsPropertyName == null ) {
final boolean isReferenceToPrimaryKey = lhsPropertyName == null;
if ( isReferenceToPrimaryKey ) {
fkTarget = collectionDescriptor.getOwnerEntityPersister().getIdentifierMapping();
}
else {
Expand All @@ -922,7 +923,8 @@ private static void interpretPluralAttributeMappingKeyDescriptor(
new SimpleForeignKeyDescriptor(
keySelectionMapping,
simpleFkTarget,
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess()
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess(),
isReferenceToPrimaryKey
)
);
}
Expand Down Expand Up @@ -1045,7 +1047,8 @@ else if ( modelPart instanceof EmbeddableValuedModelPart ) {
final ForeignKeyDescriptor foreignKeyDescriptor = new SimpleForeignKeyDescriptor(
keySelectionMapping,
simpleFkTarget,
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess()
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess(),
bootValueMapping.isReferenceToPrimaryKey()
);
attributeMapping.setForeignKeyDescriptor( foreignKeyDescriptor );
}
Expand Down
Expand Up @@ -342,7 +342,8 @@ private ForeignKeyDescriptor createForeignKeyDescriptor(
return new SimpleForeignKeyDescriptor(
keySelectionMapping,
basicFkTargetPart,
( (PropertyBasedMapping) basicFkTargetPart ).getPropertyAccess()
( (PropertyBasedMapping) basicFkTargetPart ).getPropertyAccess(),
entityType.isReferenceToPrimaryKey()
);
}
else if ( fkTargetPart instanceof EmbeddableValuedModelPart ) {
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.hibernate.metamodel.mapping.MappingType;
import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.query.ComparisonOperator;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.Clause;
Expand Down Expand Up @@ -53,15 +54,18 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
private final SelectionMapping keySelectionMapping;
private final SelectionMapping targetSelectionMapping;
private final PropertyAccess propertyAccess;
private final boolean refersToPrimaryKey;
private AssociationKey associationKey;

public SimpleForeignKeyDescriptor(
SelectionMapping keySelectionMapping,
SelectionMapping targetSelectionMapping,
PropertyAccess propertyAccess) {
PropertyAccess propertyAccess,
boolean refersToPrimaryKey) {
this.keySelectionMapping = keySelectionMapping;
this.targetSelectionMapping = targetSelectionMapping;
this.propertyAccess = propertyAccess;
this.refersToPrimaryKey = refersToPrimaryKey;
}

@Override
Expand Down Expand Up @@ -264,7 +268,18 @@ public EntityMappingType findContainingEntityMapping() {

@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
return value == null ? null : propertyAccess.getGetter().get( value );
if ( value == null ) {
return null;
}
if ( refersToPrimaryKey && value instanceof HibernateProxy ) {
return ( (HibernateProxy) value ).getHibernateLazyInitializer().getIdentifier();
}
return propertyAccess.getGetter().get( value );
}

@Override
public Object getAssociationKeyFromTarget(Object targetObject, SharedSessionContractImplementor session) {
return disassemble( targetObject, session );
}

@Override
Expand Down
Expand Up @@ -698,7 +698,7 @@ public int forEachDisassembledJdbcValue(Object value, Clause clause, int offset,

@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer consumer, SharedSessionContractImplementor session) {
return foreignKeyDescriptor.forEachJdbcValue(
return foreignKeyDescriptor.forEachDisassembledJdbcValue(
foreignKeyDescriptor.disassemble( value, session ),
clause,
offset,
Expand Down
Expand Up @@ -18,9 +18,7 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.MappingMetamodel;
import org.hibernate.metamodel.mapping.Bindable;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.MappingModelExpressable;
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
import org.hibernate.metamodel.model.domain.AllowableParameterType;
Expand Down Expand Up @@ -288,19 +286,7 @@ private static void createValueBindings(
}
else if ( type instanceof ToOneAttributeMapping ) {
ToOneAttributeMapping association = (ToOneAttributeMapping) type;
if ( association.getReferencedPropertyName() == null ) {
bindValue = association.getEntityMappingType().getIdentifierMapping().getIdentifier(
bindValue,
session
);
}
else {
bindValue = association.getEntityMappingType()
.findAttributeMapping( association.getReferencedPropertyName() )
.getPropertyAccess()
.getGetter()
.get( bindValue );
}
bindValue = association.getForeignKeyDescriptor().getAssociationKeyFromTarget( bindValue, session );
mappingExpressable = association.getForeignKeyDescriptor();
}
else {
Expand Down

0 comments on commit 06d2a0c

Please sign in to comment.