Use FK descriptor instead of target entity identifier
This commit is contained in:
parent
c58227241f
commit
06d2a0c23e
|
@ -16,6 +16,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||||
import org.hibernate.metamodel.mapping.AttributeMapping;
|
import org.hibernate.metamodel.mapping.AttributeMapping;
|
||||||
|
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
|
||||||
import org.hibernate.metamodel.mapping.SelectionConsumer;
|
import org.hibernate.metamodel.mapping.SelectionConsumer;
|
||||||
import org.hibernate.metamodel.mapping.SelectionMappings;
|
import org.hibernate.metamodel.mapping.SelectionMappings;
|
||||||
import org.hibernate.metamodel.mapping.CompositeIdentifierMapping;
|
import org.hibernate.metamodel.mapping.CompositeIdentifierMapping;
|
||||||
|
@ -202,12 +203,9 @@ public abstract class AbstractCompositeIdentifierMapping
|
||||||
final AttributeMapping attributeMapping = attributeMappings.get( i );
|
final AttributeMapping attributeMapping = attributeMappings.get( i );
|
||||||
final Object o = attributeMapping.getPropertyAccess().getGetter().get( value );
|
final Object o = attributeMapping.getPropertyAccess().getGetter().get( value );
|
||||||
if ( attributeMapping instanceof ToOneAttributeMapping ) {
|
if ( attributeMapping instanceof ToOneAttributeMapping ) {
|
||||||
final EntityMappingType associatedEntityMappingType =
|
final ForeignKeyDescriptor fkDescriptor = ( (ToOneAttributeMapping) attributeMapping ).getForeignKeyDescriptor();
|
||||||
( (ToOneAttributeMapping) attributeMapping ).getAssociatedEntityMappingType();
|
final Object identifier = fkDescriptor.getAssociationKeyFromTarget( o, session );
|
||||||
final EntityIdentifierMapping identifierMapping =
|
span += fkDescriptor.forEachJdbcValue(
|
||||||
associatedEntityMappingType.getIdentifierMapping();
|
|
||||||
final Object identifier = identifierMapping.getIdentifier( o, session );
|
|
||||||
span += identifierMapping.forEachJdbcValue(
|
|
||||||
identifier,
|
identifier,
|
||||||
clause,
|
clause,
|
||||||
span + offset,
|
span + offset,
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.metamodel.mapping;
|
package org.hibernate.metamodel.mapping;
|
||||||
|
|
||||||
|
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||||
import org.hibernate.query.NavigablePath;
|
import org.hibernate.query.NavigablePath;
|
||||||
import org.hibernate.sql.ast.SqlAstJoinType;
|
import org.hibernate.sql.ast.SqlAstJoinType;
|
||||||
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
|
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
|
||||||
|
@ -65,6 +66,8 @@ public interface ForeignKeyDescriptor extends VirtualModelPart {
|
||||||
return visitReferringColumns( offset, consumer );
|
return visitReferringColumns( offset, consumer );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object getAssociationKeyFromTarget(Object targetObject, SharedSessionContractImplementor session);
|
||||||
|
|
||||||
int visitReferringColumns(int offset, SelectionConsumer consumer);
|
int visitReferringColumns(int offset, SelectionConsumer consumer);
|
||||||
|
|
||||||
int visitTargetColumns(int offset, SelectionConsumer consumer);
|
int visitTargetColumns(int offset, SelectionConsumer consumer);
|
||||||
|
|
|
@ -331,11 +331,7 @@ public class CompoundNaturalIdMapping extends AbstractNaturalIdMapping implement
|
||||||
if ( attributeMapping instanceof ToOneAttributeMapping ) {
|
if ( attributeMapping instanceof ToOneAttributeMapping ) {
|
||||||
final ToOneAttributeMapping toOne = (ToOneAttributeMapping) attributeMapping;
|
final ToOneAttributeMapping toOne = (ToOneAttributeMapping) attributeMapping;
|
||||||
final ForeignKeyDescriptor fKDescriptor = toOne.getForeignKeyDescriptor();
|
final ForeignKeyDescriptor fKDescriptor = toOne.getForeignKeyDescriptor();
|
||||||
|
final Object keyValue = value == null ? null : fKDescriptor.disassemble( value, session );
|
||||||
final EntityMappingType associatedEntityMapping = toOne.getEntityMappingType();
|
|
||||||
final EntityIdentifierMapping associatedEntityMappingIdentifierMapping = associatedEntityMapping.getIdentifierMapping();
|
|
||||||
|
|
||||||
final Object keyValue = value == null ? null : associatedEntityMappingIdentifierMapping.getIdentifier( value, session );
|
|
||||||
fKDescriptor.breakDownJdbcValues( keyValue, valueConsumer, session );
|
fKDescriptor.breakDownJdbcValues( keyValue, valueConsumer, session );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -409,6 +409,16 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Override
|
||||||
public EntityMappingType findContainingEntityMapping() {
|
public EntityMappingType findContainingEntityMapping() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
|
|
@ -899,7 +899,8 @@ public class MappingModelCreationHelper {
|
||||||
final Type keyType = bootValueMappingKey.getType();
|
final Type keyType = bootValueMappingKey.getType();
|
||||||
final ModelPart fkTarget;
|
final ModelPart fkTarget;
|
||||||
final String lhsPropertyName = collectionDescriptor.getCollectionType().getLHSPropertyName();
|
final String lhsPropertyName = collectionDescriptor.getCollectionType().getLHSPropertyName();
|
||||||
if ( lhsPropertyName == null ) {
|
final boolean isReferenceToPrimaryKey = lhsPropertyName == null;
|
||||||
|
if ( isReferenceToPrimaryKey ) {
|
||||||
fkTarget = collectionDescriptor.getOwnerEntityPersister().getIdentifierMapping();
|
fkTarget = collectionDescriptor.getOwnerEntityPersister().getIdentifierMapping();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -922,7 +923,8 @@ public class MappingModelCreationHelper {
|
||||||
new SimpleForeignKeyDescriptor(
|
new SimpleForeignKeyDescriptor(
|
||||||
keySelectionMapping,
|
keySelectionMapping,
|
||||||
simpleFkTarget,
|
simpleFkTarget,
|
||||||
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess()
|
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess(),
|
||||||
|
isReferenceToPrimaryKey
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1045,7 +1047,8 @@ public class MappingModelCreationHelper {
|
||||||
final ForeignKeyDescriptor foreignKeyDescriptor = new SimpleForeignKeyDescriptor(
|
final ForeignKeyDescriptor foreignKeyDescriptor = new SimpleForeignKeyDescriptor(
|
||||||
keySelectionMapping,
|
keySelectionMapping,
|
||||||
simpleFkTarget,
|
simpleFkTarget,
|
||||||
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess()
|
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess(),
|
||||||
|
bootValueMapping.isReferenceToPrimaryKey()
|
||||||
);
|
);
|
||||||
attributeMapping.setForeignKeyDescriptor( foreignKeyDescriptor );
|
attributeMapping.setForeignKeyDescriptor( foreignKeyDescriptor );
|
||||||
}
|
}
|
||||||
|
|
|
@ -342,7 +342,8 @@ public class PluralAttributeMappingImpl
|
||||||
return new SimpleForeignKeyDescriptor(
|
return new SimpleForeignKeyDescriptor(
|
||||||
keySelectionMapping,
|
keySelectionMapping,
|
||||||
basicFkTargetPart,
|
basicFkTargetPart,
|
||||||
( (PropertyBasedMapping) basicFkTargetPart ).getPropertyAccess()
|
( (PropertyBasedMapping) basicFkTargetPart ).getPropertyAccess(),
|
||||||
|
entityType.isReferenceToPrimaryKey()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else if ( fkTargetPart instanceof EmbeddableValuedModelPart ) {
|
else if ( fkTargetPart instanceof EmbeddableValuedModelPart ) {
|
||||||
|
|
|
@ -24,6 +24,7 @@ import org.hibernate.metamodel.mapping.JdbcMapping;
|
||||||
import org.hibernate.metamodel.mapping.MappingType;
|
import org.hibernate.metamodel.mapping.MappingType;
|
||||||
import org.hibernate.metamodel.model.domain.NavigableRole;
|
import org.hibernate.metamodel.model.domain.NavigableRole;
|
||||||
import org.hibernate.property.access.spi.PropertyAccess;
|
import org.hibernate.property.access.spi.PropertyAccess;
|
||||||
|
import org.hibernate.proxy.HibernateProxy;
|
||||||
import org.hibernate.query.ComparisonOperator;
|
import org.hibernate.query.ComparisonOperator;
|
||||||
import org.hibernate.query.NavigablePath;
|
import org.hibernate.query.NavigablePath;
|
||||||
import org.hibernate.sql.ast.Clause;
|
import org.hibernate.sql.ast.Clause;
|
||||||
|
@ -53,15 +54,18 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
|
||||||
private final SelectionMapping keySelectionMapping;
|
private final SelectionMapping keySelectionMapping;
|
||||||
private final SelectionMapping targetSelectionMapping;
|
private final SelectionMapping targetSelectionMapping;
|
||||||
private final PropertyAccess propertyAccess;
|
private final PropertyAccess propertyAccess;
|
||||||
|
private final boolean refersToPrimaryKey;
|
||||||
private AssociationKey associationKey;
|
private AssociationKey associationKey;
|
||||||
|
|
||||||
public SimpleForeignKeyDescriptor(
|
public SimpleForeignKeyDescriptor(
|
||||||
SelectionMapping keySelectionMapping,
|
SelectionMapping keySelectionMapping,
|
||||||
SelectionMapping targetSelectionMapping,
|
SelectionMapping targetSelectionMapping,
|
||||||
PropertyAccess propertyAccess) {
|
PropertyAccess propertyAccess,
|
||||||
|
boolean refersToPrimaryKey) {
|
||||||
this.keySelectionMapping = keySelectionMapping;
|
this.keySelectionMapping = keySelectionMapping;
|
||||||
this.targetSelectionMapping = targetSelectionMapping;
|
this.targetSelectionMapping = targetSelectionMapping;
|
||||||
this.propertyAccess = propertyAccess;
|
this.propertyAccess = propertyAccess;
|
||||||
|
this.refersToPrimaryKey = refersToPrimaryKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -264,7 +268,18 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object disassemble(Object value, SharedSessionContractImplementor session) {
|
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
|
@Override
|
||||||
|
|
|
@ -698,7 +698,7 @@ public class ToOneAttributeMapping
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer consumer, SharedSessionContractImplementor session) {
|
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer consumer, SharedSessionContractImplementor session) {
|
||||||
return foreignKeyDescriptor.forEachJdbcValue(
|
return foreignKeyDescriptor.forEachDisassembledJdbcValue(
|
||||||
foreignKeyDescriptor.disassemble( value, session ),
|
foreignKeyDescriptor.disassemble( value, session ),
|
||||||
clause,
|
clause,
|
||||||
offset,
|
offset,
|
||||||
|
|
|
@ -18,9 +18,7 @@ import java.util.function.Function;
|
||||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||||
import org.hibernate.metamodel.MappingMetamodel;
|
import org.hibernate.metamodel.MappingMetamodel;
|
||||||
import org.hibernate.metamodel.mapping.Bindable;
|
|
||||||
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
|
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
|
||||||
import org.hibernate.metamodel.mapping.JdbcMapping;
|
|
||||||
import org.hibernate.metamodel.mapping.MappingModelExpressable;
|
import org.hibernate.metamodel.mapping.MappingModelExpressable;
|
||||||
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
|
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
|
||||||
import org.hibernate.metamodel.model.domain.AllowableParameterType;
|
import org.hibernate.metamodel.model.domain.AllowableParameterType;
|
||||||
|
@ -288,19 +286,7 @@ public class SqmUtil {
|
||||||
}
|
}
|
||||||
else if ( type instanceof ToOneAttributeMapping ) {
|
else if ( type instanceof ToOneAttributeMapping ) {
|
||||||
ToOneAttributeMapping association = (ToOneAttributeMapping) type;
|
ToOneAttributeMapping association = (ToOneAttributeMapping) type;
|
||||||
if ( association.getReferencedPropertyName() == null ) {
|
bindValue = association.getForeignKeyDescriptor().getAssociationKeyFromTarget( bindValue, session );
|
||||||
bindValue = association.getEntityMappingType().getIdentifierMapping().getIdentifier(
|
|
||||||
bindValue,
|
|
||||||
session
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bindValue = association.getEntityMappingType()
|
|
||||||
.findAttributeMapping( association.getReferencedPropertyName() )
|
|
||||||
.getPropertyAccess()
|
|
||||||
.getGetter()
|
|
||||||
.get( bindValue );
|
|
||||||
}
|
|
||||||
mappingExpressable = association.getForeignKeyDescriptor();
|
mappingExpressable = association.getForeignKeyDescriptor();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Reference in New Issue