re-enable tests

re-organize some tests
clean-up and tests related to fks
This commit is contained in:
Steve Ebersole 2021-04-01 06:54:57 -05:00
parent 1a9079006c
commit af891c0997
21 changed files with 559 additions and 125 deletions

View File

@ -124,7 +124,7 @@ public abstract class AbstractCompositeIdentifierMapping
final SingularAttributeMapping attribute = attributes.get( i ); final SingularAttributeMapping attribute = attributes.get( i );
if ( attribute instanceof ToOneAttributeMapping ) { if ( attribute instanceof ToOneAttributeMapping ) {
final ToOneAttributeMapping associationAttributeMapping = (ToOneAttributeMapping) attribute; final ToOneAttributeMapping associationAttributeMapping = (ToOneAttributeMapping) attribute;
span += associationAttributeMapping.getForeignKeyDescriptor().visitReferringSelectables( span += associationAttributeMapping.getForeignKeyDescriptor().visitKeySelectables(
span + offset, span + offset,
consumer consumer
); );

View File

@ -594,8 +594,8 @@ public class EmbeddableMappingType implements ManagedMappingType, SelectableMapp
} }
@Override @Override
public void forEachSelectable(SelectableConsumer consumer) { public int forEachSelectable(SelectableConsumer consumer) {
selectableMappings.forEachSelectable( 0, consumer ); return selectableMappings.forEachSelectable( 0, consumer );
} }
@Override @Override

View File

@ -18,14 +18,14 @@ import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.DomainResultCreationState;
/** /**
* @author Steve Ebersole * Descriptor for foreign-keys
*/ */
public interface ForeignKeyDescriptor extends VirtualModelPart { public interface ForeignKeyDescriptor extends VirtualModelPart {
String PART_NAME = "{fk}"; String PART_NAME = "{fk}";
String getKeyColumnContainingTable(); String getKeyTable();
String getTargetColumnContainingTable(); String getTargetTable();
DomainResult createCollectionFetchDomainResult( DomainResult createCollectionFetchDomainResult(
NavigablePath collectionPath, NavigablePath collectionPath,
@ -67,15 +67,15 @@ public interface ForeignKeyDescriptor extends VirtualModelPart {
*/ */
@Override @Override
default int forEachSelectable(int offset, SelectableConsumer consumer) { default int forEachSelectable(int offset, SelectableConsumer consumer) {
return visitReferringSelectables( offset, consumer ); return visitKeySelectables( offset, consumer );
} }
Object getAssociationKeyFromTarget(Object targetObject, SharedSessionContractImplementor session); Object getAssociationKeyFromTarget(Object targetObject, SharedSessionContractImplementor session);
int visitReferringSelectables(int offset, SelectableConsumer consumer); int visitKeySelectables(int offset, SelectableConsumer consumer);
default int visitReferringSelectables(SelectableConsumer consumer) { default int visitKeySelectables(SelectableConsumer consumer) {
return visitReferringSelectables( 0, consumer ); return visitKeySelectables( 0, consumer );
} }
int visitTargetSelectables(int offset, SelectableConsumer consumer); int visitTargetSelectables(int offset, SelectableConsumer consumer);

View File

@ -96,8 +96,8 @@ public interface ModelPart extends MappingModelExpressable {
throw new NotYetImplementedFor6Exception( getClass() ); throw new NotYetImplementedFor6Exception( getClass() );
} }
default void forEachSelectable(SelectableConsumer consumer) { default int forEachSelectable(SelectableConsumer consumer) {
forEachSelectable( 0, consumer ); return forEachSelectable( 0, consumer );
} }
default int forEachSelectable(int offset, SelectableConsumer consumer) { default int forEachSelectable(int offset, SelectableConsumer consumer) {

View File

@ -40,9 +40,10 @@ public interface SelectableMappings {
/** /**
* Same as {@link #forEachSelectable(int, SelectableConsumer)}, with * Same as {@link #forEachSelectable(int, SelectableConsumer)}, with
* an implicit offset of `0` * an implicit offset of `0`
* @return
*/ */
default void forEachSelectable(SelectableConsumer consumer) { default int forEachSelectable(SelectableConsumer consumer) {
forEachSelectable( 0, consumer ); return forEachSelectable( 0, consumer );
} }
/** /**

View File

@ -19,7 +19,6 @@ import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor; import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.MappingType; import org.hibernate.metamodel.mapping.MappingType;
import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.SelectableConsumer; import org.hibernate.metamodel.mapping.SelectableConsumer;
import org.hibernate.metamodel.mapping.SelectableMappings; import org.hibernate.metamodel.mapping.SelectableMappings;
import org.hibernate.metamodel.model.domain.NavigableRole; import org.hibernate.metamodel.model.domain.NavigableRole;
@ -46,25 +45,25 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/** /**
* @author Andrea Boriero * @author Andrea Boriero
*/ */
public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, ModelPart { public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor {
private final EmbeddableValuedModelPart mappingType; private final EmbeddableValuedModelPart mappingType;
private final String keyColumnContainingTable; private final String keyTable;
private final SelectableMappings keySelectableMappings; private final SelectableMappings keySelectableMappings;
private final String targetColumnContainingTable; private final String targetTable;
private final SelectableMappings targetSelectableMappings; private final SelectableMappings targetSelectableMappings;
private AssociationKey associationKey; private AssociationKey associationKey;
public EmbeddedForeignKeyDescriptor( public EmbeddedForeignKeyDescriptor(
EmbeddableValuedModelPart mappingType, EmbeddableValuedModelPart mappingType,
String keyColumnContainingTable, String keyTable,
SelectableMappings keySelectableMappings, SelectableMappings keySelectableMappings,
String targetColumnContainingTable, String targetTable,
SelectableMappings targetSelectableMappings, SelectableMappings targetSelectableMappings,
MappingModelCreationProcess creationProcess) { MappingModelCreationProcess creationProcess) {
this.keyColumnContainingTable = keyColumnContainingTable; this.keyTable = keyTable;
this.keySelectableMappings = keySelectableMappings; this.keySelectableMappings = keySelectableMappings;
this.targetColumnContainingTable = targetColumnContainingTable; this.targetTable = targetTable;
this.targetSelectableMappings = targetSelectableMappings; this.targetSelectableMappings = targetSelectableMappings;
this.mappingType = mappingType; this.mappingType = mappingType;
@ -85,13 +84,13 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
} }
@Override @Override
public String getKeyColumnContainingTable() { public String getKeyTable() {
return keyColumnContainingTable; return keyTable;
} }
@Override @Override
public String getTargetColumnContainingTable() { public String getTargetTable() {
return targetColumnContainingTable; return targetTable;
} }
@Override @Override
@ -99,11 +98,11 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
NavigablePath collectionPath, NavigablePath collectionPath,
TableGroup tableGroup, TableGroup tableGroup,
DomainResultCreationState creationState) { DomainResultCreationState creationState) {
if ( targetColumnContainingTable.equals( keyColumnContainingTable ) ) { if ( targetTable.equals( keyTable ) ) {
return createDomainResult( return createDomainResult(
collectionPath, collectionPath,
tableGroup, tableGroup,
targetColumnContainingTable, targetTable,
targetSelectableMappings, targetSelectableMappings,
creationState creationState
); );
@ -112,7 +111,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
return createDomainResult( return createDomainResult(
collectionPath, collectionPath,
tableGroup, tableGroup,
keyColumnContainingTable, keyTable,
keySelectableMappings, keySelectableMappings,
creationState creationState
); );
@ -127,7 +126,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
return createDomainResult( return createDomainResult(
collectionPath, collectionPath,
tableGroup, tableGroup,
keyColumnContainingTable, keyTable,
keySelectableMappings, keySelectableMappings,
creationState creationState
); );
@ -143,7 +142,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
return createDomainResult( return createDomainResult(
collectionPath, collectionPath,
tableGroup, tableGroup,
keyColumnContainingTable, keyTable,
keySelectableMappings, keySelectableMappings,
creationState creationState
); );
@ -152,7 +151,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
return createDomainResult( return createDomainResult(
collectionPath, collectionPath,
tableGroup, tableGroup,
targetColumnContainingTable, targetTable,
targetSelectableMappings, targetSelectableMappings,
creationState creationState
); );
@ -213,22 +212,22 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
SqlAstCreationContext creationContext) { SqlAstCreationContext creationContext) {
TableReference lhsTableReference; TableReference lhsTableReference;
TableReference rhsTableKeyReference; TableReference rhsTableKeyReference;
if ( targetColumnContainingTable.equals( keyColumnContainingTable ) ) { if ( targetTable.equals( keyTable ) ) {
lhsTableReference = getTableReferenceWhenTargetEqualsKey( lhs, tableGroup, keyColumnContainingTable ); lhsTableReference = getTableReferenceWhenTargetEqualsKey( lhs, tableGroup, keyTable );
rhsTableKeyReference = getTableReference( rhsTableKeyReference = getTableReference(
lhs, lhs,
tableGroup, tableGroup,
targetColumnContainingTable targetTable
); );
} }
else { else {
lhsTableReference = getTableReference( lhs, tableGroup, keyColumnContainingTable ); lhsTableReference = getTableReference( lhs, tableGroup, keyTable );
rhsTableKeyReference = getTableReference( rhsTableKeyReference = getTableReference(
lhs, lhs,
tableGroup, tableGroup,
targetColumnContainingTable targetTable
); );
} }
@ -250,12 +249,12 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
SqlAstCreationContext creationContext) { SqlAstCreationContext creationContext) {
final String rhsTableExpression = rhs.getTableExpression(); final String rhsTableExpression = rhs.getTableExpression();
final String lhsTableExpression = lhs.getTableExpression(); final String lhsTableExpression = lhs.getTableExpression();
if ( lhsTableExpression.equals( keyColumnContainingTable ) ) { if ( lhsTableExpression.equals( keyTable ) ) {
assert rhsTableExpression.equals( targetColumnContainingTable ); assert rhsTableExpression.equals( targetTable );
return getPredicate( lhs, rhs, creationContext, keySelectableMappings, targetSelectableMappings ); return getPredicate( lhs, rhs, creationContext, keySelectableMappings, targetSelectableMappings );
} }
else { else {
assert rhsTableExpression.equals( keyColumnContainingTable ); assert rhsTableExpression.equals( keyTable );
return getPredicate( lhs, rhs, creationContext, targetSelectableMappings, keySelectableMappings ); return getPredicate( lhs, rhs, creationContext, targetSelectableMappings, keySelectableMappings );
} }
} }
@ -322,7 +321,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
} }
@Override @Override
public int visitReferringSelectables(int offset, SelectableConsumer consumer) { public int visitKeySelectables(int offset, SelectableConsumer consumer) {
return keySelectableMappings.forEachSelectable( offset, consumer ); return keySelectableMappings.forEachSelectable( offset, consumer );
} }
@ -340,7 +339,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
columns.add( selection.getSelectionExpression() ); columns.add( selection.getSelectionExpression() );
} }
); );
associationKey = new AssociationKey( keyColumnContainingTable, columns ); associationKey = new AssociationKey( keyTable, columns );
} }
return associationKey; return associationKey;
} }
@ -369,7 +368,7 @@ public class EmbeddedForeignKeyDescriptor implements ForeignKeyDescriptor, Model
//noinspection unchecked //noinspection unchecked
final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState(); final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver(); final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
final TableReference tableReference = tableGroup.resolveTableReference( keyColumnContainingTable ); final TableReference tableReference = tableGroup.resolveTableReference( keyTable );
final String identificationVariable = tableReference.getIdentificationVariable(); final String identificationVariable = tableReference.getIdentificationVariable();
final int size = keySelectableMappings.getJdbcTypeCount(); final int size = keySelectableMappings.getJdbcTypeCount();
final List<SqlSelection> sqlSelections = new ArrayList<>( size ); final List<SqlSelection> sqlSelections = new ArrayList<>( size );

View File

@ -222,8 +222,43 @@ public class EntityCollectionPart
// //
// // todo (6.0) : do we need to make the FK table/columns available as well from this table group? // // todo (6.0) : do we need to make the FK table/columns available as well from this table group?
// //
// final TableReference fkReferringTable = parentTableGroup.resolveTableReference( foreignKeyDescriptor.getKeyColumnContainingTable() ); // final TableReference fkReferringTable;
// final TableReference fkTargetTable = entityTableGroup.resolveTableReference( foreignKeyDescriptor.getTargetColumnContainingTable() ); // try {
// fkReferringTable = parentTableGroup.resolveTableReference( foreignKeyDescriptor.getKeyTable() );
// }
// catch (HibernateException e) {
// throw e;
// }
// catch (Exception e) {
// throw new SqlTreeCreationException(
// String.format(
// Locale.ROOT,
// "Unable to locate `%s` as foreign-key referring table for entity collection part `%s` relative to parent TableGroup : %s",
// foreignKeyDescriptor.getKeyTable(),
// navigableRole.getFullPath(),
// parentTableGroup
// )
// );
// }
//
// final TableReference fkTargetTable;
// try {
// fkTargetTable = entityTableGroup.resolveTableReference( foreignKeyDescriptor.getTargetTable() );
// }
// catch (HibernateException e) {
// throw e;
// }
// catch (Exception e) {
// throw new SqlTreeCreationException(
// String.format(
// Locale.ROOT,
// "Unable to locate `%s` as foreign-key target table for entity collection part `%s` relative to rhs TableGroup : %s",
// foreignKeyDescriptor.getTargetTable(),
// navigableRole.getFullPath(),
// entityTableGroup
// )
// );
// }
// //
// return new TableGroupJoin( // return new TableGroupJoin(
// navigablePath, // navigablePath,

View File

@ -923,7 +923,7 @@ public class MappingModelCreationHelper {
new SimpleForeignKeyDescriptor( new SimpleForeignKeyDescriptor(
keySelectableMapping, keySelectableMapping,
simpleFkTarget, simpleFkTarget,
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess(), (owner) -> ( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess().getGetter().get( owner ),
isReferenceToPrimaryKey isReferenceToPrimaryKey
) )
); );
@ -946,17 +946,19 @@ public class MappingModelCreationHelper {
} }
} }
public static void interpretSingularAssociationAttributeMappingKeyDescriptor( public static void interpretToOneKeyDescriptor(
ToOneAttributeMapping attributeMapping, ToOneAttributeMapping attributeMapping,
Property bootProperty, Property bootProperty,
ToOne bootValueMapping, ToOne bootValueMapping,
Dialect dialect, Dialect dialect,
MappingModelCreationProcess creationProcess) { MappingModelCreationProcess creationProcess) {
if ( attributeMapping.getForeignKeyDescriptor() != null ) { if ( attributeMapping.getForeignKeyDescriptor() != null ) {
// already built/known
return; return;
} }
final String tableName = getTableIdentifierExpression( bootValueMapping.getTable(), creationProcess ); final String tableName = getTableIdentifierExpression( bootValueMapping.getTable(), creationProcess );
attributeMapping.setIdentifyingColumnsTableExpression( tableName ); attributeMapping.setIdentifyingColumnsTableExpression( tableName );
final EntityPersister referencedEntityDescriptor = creationProcess final EntityPersister referencedEntityDescriptor = creationProcess
@ -998,7 +1000,7 @@ public class MappingModelCreationHelper {
} }
else { else {
throw new NotYetImplementedFor6Exception( throw new NotYetImplementedFor6Exception(
"Support for composite foreign-keys not yet implemented: " + "Support for foreign-keys based on `" + modelPart + "` not yet implemented: " +
bootProperty.getPersistentClass().getEntityName() + " -> " + bootProperty.getName() bootProperty.getPersistentClass().getEntityName() + " -> " + bootProperty.getName()
); );
} }
@ -1047,7 +1049,7 @@ public class MappingModelCreationHelper {
final ForeignKeyDescriptor foreignKeyDescriptor = new SimpleForeignKeyDescriptor( final ForeignKeyDescriptor foreignKeyDescriptor = new SimpleForeignKeyDescriptor(
keySelectableMapping, keySelectableMapping,
simpleFkTarget, simpleFkTarget,
( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess(), (owner) -> ( (PropertyBasedMapping) simpleFkTarget ).getPropertyAccess().getGetter().get( owner ),
bootValueMapping.isReferenceToPrimaryKey() bootValueMapping.isReferenceToPrimaryKey()
); );
attributeMapping.setForeignKeyDescriptor( foreignKeyDescriptor ); attributeMapping.setForeignKeyDescriptor( foreignKeyDescriptor );
@ -1154,7 +1156,7 @@ public class MappingModelCreationHelper {
.getEntityBinding( .getEntityBinding(
referencedEntityDescriptor.getEntityName() ); referencedEntityDescriptor.getEntityName() );
Property property = entityBinding.getProperty( referencedPropertyName ); Property property = entityBinding.getProperty( referencedPropertyName );
interpretSingularAssociationAttributeMappingKeyDescriptor( interpretToOneKeyDescriptor(
referencedAttributeMapping, referencedAttributeMapping,
property, property,
(ToOne) property.getValue(), (ToOne) property.getValue(),
@ -1173,8 +1175,7 @@ public class MappingModelCreationHelper {
.getMetadata() .getMetadata()
.getDatabase() .getDatabase()
.getJdbcEnvironment(); .getJdbcEnvironment();
return jdbcEnvironment return jdbcEnvironment.getQualifiedObjectNameFormatter().format(
.getQualifiedObjectNameFormatter().format(
table.getQualifiedTableName(), table.getQualifiedTableName(),
jdbcEnvironment.getDialect() jdbcEnvironment.getDialect()
); );
@ -1469,7 +1470,7 @@ public class MappingModelCreationHelper {
.getJdbcServices() .getJdbcServices()
.getDialect(); .getDialect();
MappingModelCreationHelper.interpretSingularAssociationAttributeMappingKeyDescriptor( MappingModelCreationHelper.interpretToOneKeyDescriptor(
attributeMapping, attributeMapping,
bootProperty, bootProperty,
(ToOne) bootProperty.getValue(), (ToOne) bootProperty.getValue(),

View File

@ -341,7 +341,7 @@ public class PluralAttributeMappingImpl
return new SimpleForeignKeyDescriptor( return new SimpleForeignKeyDescriptor(
keySelectableMapping, keySelectableMapping,
basicFkTargetPart, basicFkTargetPart,
( (PropertyBasedMapping) basicFkTargetPart ).getPropertyAccess(), (owner) -> ( (PropertyBasedMapping) basicFkTargetPart ).getPropertyAccess().getGetter().get( owner ),
entityType.isReferenceToPrimaryKey() entityType.isReferenceToPrimaryKey()
); );
} }

View File

@ -8,25 +8,31 @@ package org.hibernate.metamodel.mapping.internal;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Function;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.engine.FetchStyle; import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming; import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.mapping.IndexedConsumer; import org.hibernate.mapping.IndexedConsumer;
import org.hibernate.metamodel.mapping.AssociationKey; import org.hibernate.metamodel.mapping.AssociationKey;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.BasicValuedModelPart; import org.hibernate.metamodel.mapping.BasicValuedModelPart;
import org.hibernate.metamodel.mapping.SelectableConsumer;
import org.hibernate.metamodel.mapping.SelectableMapping;
import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor; import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.MappingModelExpressable;
import org.hibernate.metamodel.mapping.MappingType; import org.hibernate.metamodel.mapping.MappingType;
import org.hibernate.metamodel.mapping.SelectableConsumer;
import org.hibernate.metamodel.mapping.SelectableMapping;
import org.hibernate.metamodel.mapping.SelectableMappings;
import org.hibernate.metamodel.mapping.ValueMapping;
import org.hibernate.metamodel.model.domain.NavigableRole; import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.proxy.HibernateProxy; 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.query.sqm.sql.internal.DomainResultProducer;
import org.hibernate.sql.ast.Clause; import org.hibernate.sql.ast.Clause;
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;
@ -51,31 +57,46 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicValuedModelPart, FetchOptions { public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicValuedModelPart, FetchOptions {
private final SelectableMapping keySelectableMapping; private final SideModelPart keySide;
private final SelectableMapping targetSelectableMapping; private final SideModelPart targetSide;
private final PropertyAccess propertyAccess;
private final boolean refersToPrimaryKey; private final boolean refersToPrimaryKey;
private final Function<Object,Object> disassemblyValueExtractor;
private AssociationKey associationKey; private AssociationKey associationKey;
public SimpleForeignKeyDescriptor( public SimpleForeignKeyDescriptor(
SelectableMapping keySelectableMapping, SelectableMapping keySelectableMapping,
SelectableMapping targetSelectableMapping, SelectableMapping targetSelectableMapping,
PropertyAccess propertyAccess, Function<Object,Object> disassemblyValueExtractor,
boolean refersToPrimaryKey) { boolean refersToPrimaryKey) {
this.keySelectableMapping = keySelectableMapping; assert keySelectableMapping != null;
this.targetSelectableMapping = targetSelectableMapping; assert targetSelectableMapping != null;
this.propertyAccess = propertyAccess; assert disassemblyValueExtractor != null;
this.keySide = new SideModelPart( keySelectableMapping );
this.targetSide = new SideModelPart( targetSelectableMapping );
this.disassemblyValueExtractor = disassemblyValueExtractor;
this.refersToPrimaryKey = refersToPrimaryKey; this.refersToPrimaryKey = refersToPrimaryKey;
} }
@Override @Override
public String getKeyColumnContainingTable() { public String getKeyTable() {
return keySelectableMapping.getContainingTableExpression(); return keySide.selectableMapping.getContainingTableExpression();
} }
@Override @Override
public String getTargetColumnContainingTable() { public String getTargetTable() {
return targetSelectableMapping.getContainingTableExpression(); return targetSide.selectableMapping.getContainingTableExpression();
}
public SideModelPart getKeySide() {
return keySide;
}
public SideModelPart getTargetSide() {
return targetSide;
} }
@Override @Override
@ -83,31 +104,31 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
NavigablePath collectionPath, NavigablePath collectionPath,
TableGroup tableGroup, TableGroup tableGroup,
DomainResultCreationState creationState) { DomainResultCreationState creationState) {
if ( targetSelectableMapping.getContainingTableExpression() if ( targetSide.selectableMapping.getContainingTableExpression()
.equals( keySelectableMapping.getContainingTableExpression() ) ) { .equals( keySide.selectableMapping.getContainingTableExpression() ) ) {
return createDomainResult( tableGroup, targetSelectableMapping, creationState ); return createDomainResult( tableGroup, targetSide.selectableMapping, creationState );
} }
return createDomainResult( collectionPath, tableGroup, creationState ); return createDomainResult( collectionPath, tableGroup, creationState );
} }
@Override @Override
public DomainResult createDomainResult( public DomainResult<?> createDomainResult(
NavigablePath navigablePath, NavigablePath navigablePath,
TableGroup tableGroup, TableGroup tableGroup,
DomainResultCreationState creationState) { DomainResultCreationState creationState) {
return createDomainResult( tableGroup, keySelectableMapping, creationState ); return createDomainResult( tableGroup, keySide.selectableMapping, creationState );
} }
@Override @Override
public DomainResult createDomainResult( public DomainResult<?> createDomainResult(
NavigablePath navigablePath, NavigablePath navigablePath,
TableGroup tableGroup, TableGroup tableGroup,
boolean isKeyReferringSide, boolean isKeyReferringSide,
DomainResultCreationState creationState) { DomainResultCreationState creationState) {
if ( isKeyReferringSide ) { if ( isKeyReferringSide ) {
return createDomainResult( tableGroup, keySelectableMapping, creationState ); return createDomainResult( tableGroup, keySide.selectableMapping, creationState );
} }
return createDomainResult( tableGroup, targetSelectableMapping, creationState ); return createDomainResult( tableGroup, targetSide.selectableMapping, creationState );
} }
@Override @Override
@ -116,7 +137,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
TableGroup tableGroup, TableGroup tableGroup,
String resultVariable, String resultVariable,
DomainResultCreationState creationState) { DomainResultCreationState creationState) {
return createDomainResult( tableGroup, keySelectableMapping, creationState ); return createDomainResult( tableGroup, keySide.selectableMapping, creationState );
} }
private <T> DomainResult<T> createDomainResult( private <T> DomainResult<T> createDomainResult(
@ -145,7 +166,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
); );
//noinspection unchecked //noinspection unchecked
return new BasicResult( return new BasicResult<T>(
sqlSelection.getValuesArrayPosition(), sqlSelection.getValuesArrayPosition(),
null, null,
selectableMapping.getJdbcMapping().getJavaTypeDescriptor() selectableMapping.getJdbcMapping().getJavaTypeDescriptor()
@ -159,17 +180,17 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
SqlAstJoinType sqlAstJoinType, SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver, SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) { SqlAstCreationContext creationContext) {
if ( lhs.getTableReference( keySelectableMapping.getContainingTableExpression() ) != null ) { if ( lhs.getTableReference( keySide.selectableMapping.getContainingTableExpression() ) != null ) {
return new ComparisonPredicate( return new ComparisonPredicate(
new ColumnReference( new ColumnReference(
lhs, lhs,
keySelectableMapping, keySide.selectableMapping,
creationContext.getSessionFactory() creationContext.getSessionFactory()
), ),
ComparisonOperator.EQUAL, ComparisonOperator.EQUAL,
new ColumnReference( new ColumnReference(
rhs, rhs,
targetSelectableMapping, targetSide.selectableMapping,
creationContext.getSessionFactory() creationContext.getSessionFactory()
) )
); );
@ -178,13 +199,13 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
return new ComparisonPredicate( return new ComparisonPredicate(
new ColumnReference( new ColumnReference(
lhs, lhs,
targetSelectableMapping, targetSide.selectableMapping,
creationContext.getSessionFactory() creationContext.getSessionFactory()
), ),
ComparisonOperator.EQUAL, ComparisonOperator.EQUAL,
new ColumnReference( new ColumnReference(
rhs, rhs,
keySelectableMapping, keySide.selectableMapping,
creationContext.getSessionFactory() creationContext.getSessionFactory()
) )
); );
@ -200,22 +221,22 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
SqlAstCreationContext creationContext) { SqlAstCreationContext creationContext) {
TableReference lhsTableReference; TableReference lhsTableReference;
TableReference rhsTableKeyReference; TableReference rhsTableKeyReference;
if ( targetSelectableMapping.getContainingTableExpression().equals( keySelectableMapping.getContainingTableExpression() ) ) { if ( targetSide.selectableMapping.getContainingTableExpression().equals( keySide.selectableMapping.getContainingTableExpression() ) ) {
lhsTableReference = getTableReferenceWhenTargetEqualsKey( lhs, tableGroup, keySelectableMapping.getContainingTableExpression() ); lhsTableReference = getTableReferenceWhenTargetEqualsKey( lhs, tableGroup, keySide.selectableMapping.getContainingTableExpression() );
rhsTableKeyReference = getTableReference( rhsTableKeyReference = getTableReference(
lhs, lhs,
tableGroup, tableGroup,
targetSelectableMapping.getContainingTableExpression() targetSide.selectableMapping.getContainingTableExpression()
); );
} }
else { else {
lhsTableReference = getTableReference( lhs, tableGroup, keySelectableMapping.getContainingTableExpression() ); lhsTableReference = getTableReference( lhs, tableGroup, keySide.selectableMapping.getContainingTableExpression() );
rhsTableKeyReference = getTableReference( rhsTableKeyReference = getTableReference(
lhs, lhs,
tableGroup, tableGroup,
targetSelectableMapping.getContainingTableExpression() targetSide.selectableMapping.getContainingTableExpression()
); );
} }
@ -263,7 +284,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
@Override @Override
public JavaTypeDescriptor<?> getJavaTypeDescriptor() { public JavaTypeDescriptor<?> getJavaTypeDescriptor() {
return targetSelectableMapping.getJdbcMapping().getJavaTypeDescriptor(); return targetSide.getJdbcMapping().getJavaTypeDescriptor();
} }
@Override @Override
@ -284,7 +305,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
if ( refersToPrimaryKey && value instanceof HibernateProxy ) { if ( refersToPrimaryKey && value instanceof HibernateProxy ) {
return ( (HibernateProxy) value ).getHibernateLazyInitializer().getIdentifier(); return ( (HibernateProxy) value ).getHibernateLazyInitializer().getIdentifier();
} }
return propertyAccess.getGetter().get( value ); return disassemblyValueExtractor.apply( value );
} }
@Override @Override
@ -305,38 +326,38 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
@Override @Override
public void breakDownJdbcValues(Object domainValue, JdbcValueConsumer valueConsumer, SharedSessionContractImplementor session) { public void breakDownJdbcValues(Object domainValue, JdbcValueConsumer valueConsumer, SharedSessionContractImplementor session) {
valueConsumer.consume( domainValue, keySelectableMapping ); valueConsumer.consume( domainValue, keySide.selectableMapping );
} }
@Override @Override
public int visitReferringSelectables(int offset, SelectableConsumer consumer) { public int visitKeySelectables(int offset, SelectableConsumer consumer) {
consumer.accept( offset, keySelectableMapping ); consumer.accept( offset, keySide.selectableMapping );
return getJdbcTypeCount(); return getJdbcTypeCount();
} }
@Override @Override
public int visitTargetSelectables(int offset, SelectableConsumer consumer) { public int visitTargetSelectables(int offset, SelectableConsumer consumer) {
consumer.accept( offset, targetSelectableMapping ); consumer.accept( offset, targetSide.selectableMapping );
return getJdbcTypeCount(); return getJdbcTypeCount();
} }
@Override @Override
public AssociationKey getAssociationKey() { public AssociationKey getAssociationKey() {
if ( associationKey == null ) { if ( associationKey == null ) {
final List<String> associationKeyColumns = Collections.singletonList( keySelectableMapping.getSelectionExpression() ); final List<String> associationKeyColumns = Collections.singletonList( keySide.selectableMapping.getSelectionExpression() );
associationKey = new AssociationKey( keySelectableMapping.getContainingTableExpression(), associationKeyColumns ); associationKey = new AssociationKey( keySide.selectableMapping.getContainingTableExpression(), associationKeyColumns );
} }
return associationKey; return associationKey;
} }
@Override @Override
public List<JdbcMapping> getJdbcMappings() { public List<JdbcMapping> getJdbcMappings() {
return Collections.singletonList( targetSelectableMapping.getJdbcMapping() ); return Collections.singletonList( targetSide.getJdbcMapping() );
} }
@Override @Override
public int forEachJdbcType(int offset, IndexedConsumer<JdbcMapping> action) { public int forEachJdbcType(int offset, IndexedConsumer<JdbcMapping> action) {
action.accept( offset, targetSelectableMapping.getJdbcMapping() ); action.accept( offset, targetSide.getJdbcMapping() );
return getJdbcTypeCount(); return getJdbcTypeCount();
} }
@ -347,34 +368,34 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
int offset, int offset,
JdbcValuesConsumer valuesConsumer, JdbcValuesConsumer valuesConsumer,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
valuesConsumer.consume( offset, value, targetSelectableMapping.getJdbcMapping() ); valuesConsumer.consume( offset, value, targetSide.getJdbcMapping() );
return getJdbcTypeCount(); return getJdbcTypeCount();
} }
@Override @Override
public String getContainingTableExpression() { public String getContainingTableExpression() {
return keySelectableMapping.getContainingTableExpression(); return keySide.selectableMapping.getContainingTableExpression();
} }
@Override @Override
public String getSelectionExpression() { public String getSelectionExpression() {
return keySelectableMapping.getSelectionExpression(); return keySide.selectableMapping.getSelectionExpression();
} }
@Override @Override
public boolean isFormula() { public boolean isFormula() {
return keySelectableMapping.isFormula(); return keySide.selectableMapping.isFormula();
} }
@Override @Override
public String getCustomReadExpression() { public String getCustomReadExpression() {
return keySelectableMapping.getCustomReadExpression(); return keySide.selectableMapping.getCustomReadExpression();
} }
@Override @Override
public String getCustomWriteExpression() { public String getCustomWriteExpression() {
return keySelectableMapping.getCustomWriteExpression(); return keySide.selectableMapping.getCustomWriteExpression();
} }
@Override @Override
@ -416,13 +437,77 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
@Override @Override
public JdbcMapping getJdbcMapping() { public JdbcMapping getJdbcMapping() {
return keySelectableMapping.getJdbcMapping(); return keySide.getJdbcMapping();
} }
@Override @Override
public String toString() { public String toString() {
return "SimpleForeignKeyDescriptor : " + keySelectableMapping.getContainingTableExpression() + "." + keySelectableMapping return String.format(
.getSelectionExpression() "SimpleForeignKeyDescriptor : %s.%s -> %s.%s",
+ " --> " + targetSelectableMapping.getContainingTableExpression() + "." + targetSelectableMapping.getSelectionExpression(); keySide.selectableMapping.getContainingTableExpression(),
keySide.selectableMapping.getSelectionExpression(),
targetSide.selectableMapping.getContainingTableExpression(),
targetSide.selectableMapping.getSelectionExpression()
);
}
private interface KeySideModelPart extends MappingModelExpressable, DomainResultProducer, SelectableMappings {
@Override
default int getJdbcTypeCount() {
throw new NotYetImplementedFor6Exception( getClass() );
}
@Override
default List<JdbcMapping> getJdbcMappings() {
throw new NotYetImplementedFor6Exception( getClass() );
}
}
public static class SideModelPart implements BasicValuedMapping, KeySideModelPart {
private final SelectableMapping selectableMapping;
private final List<JdbcMapping> jdbcMappings;
public SideModelPart(SelectableMapping selectableMapping) {
assert selectableMapping != null;
this.selectableMapping = selectableMapping;
this.jdbcMappings = Collections.singletonList( getJdbcMapping() );
}
public SelectableMapping getSelectableMapping() {
return selectableMapping;
}
@Override
public int getJdbcTypeCount() {
return 1;
}
@Override
public List<JdbcMapping> getJdbcMappings() {
return jdbcMappings;
}
@Override
public JdbcMapping getJdbcMapping() {
return selectableMapping.getJdbcMapping();
}
@Override
public MappingType getMappedType() {
return getJdbcMapping();
}
@Override
public SelectableMapping getSelectable(int columnIndex) {
assert columnIndex == 0;
return selectableMapping;
}
@Override
public int forEachSelectable(int offset, SelectableConsumer consumer) {
consumer.accept( offset, selectableMapping );
return 1;
}
} }
} }

View File

@ -87,8 +87,9 @@ public class SingleSelectableMappings implements SelectableMapping, SelectableMa
} }
@Override @Override
public void forEachSelectable(SelectableConsumer consumer) { public int forEachSelectable(SelectableConsumer consumer) {
consumer.accept( 0, this ); consumer.accept( 0, this );
return 1;
} }
@Override @Override

View File

@ -669,7 +669,7 @@ public class ToOneAttributeMapping
@Override @Override
public int forEachSelectable(int offset, SelectableConsumer consumer) { public int forEachSelectable(int offset, SelectableConsumer consumer) {
if ( isKeyReferringSide ) { if ( isKeyReferringSide ) {
return foreignKeyDescriptor.visitReferringSelectables( offset, consumer ); return foreignKeyDescriptor.visitKeySelectables( offset, consumer );
} }
else { else {
return 0; return 0;

View File

@ -74,7 +74,7 @@ public class CteDeleteHandler extends AbstractCteMutationHandler implements Dele
); );
final TableReference dmlTableReference = new TableReference( tableExpression, null, true, factory ); final TableReference dmlTableReference = new TableReference( tableExpression, null, true, factory );
final List<ColumnReference> columnReferences = new ArrayList<>( idSelectCte.getCteTable().getCteColumns().size() ); final List<ColumnReference> columnReferences = new ArrayList<>( idSelectCte.getCteTable().getCteColumns().size() );
pluralAttribute.getKeyDescriptor().visitReferringSelectables( pluralAttribute.getKeyDescriptor().visitKeySelectables(
(index, selectable) -> columnReferences.add( (index, selectable) -> columnReferences.add(
new ColumnReference( new ColumnReference(
dmlTableReference, dmlTableReference,

View File

@ -36,4 +36,9 @@ public class SqlAliasBaseImpl implements SqlAliasBase {
return alias; return alias;
} }
} }
@Override
public String toString() {
return "SqlAliasBase(" + stem + " : " + aliasCount + ")";
}
} }

View File

@ -0,0 +1,307 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.constraint;
import java.util.Iterator;
import java.util.List;
import javax.persistence.CollectionTable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.JoinedSubclass;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.PrimaryKey;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Selectable;
import org.hibernate.mapping.Subclass;
import org.hibernate.mapping.ToOne;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.hamcrest.CollectionMatchers;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.DomainModelScope;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
* Simplified form of mappings from {@link ForeignKeyNoConstraintTest}
* specifically for checking definition of foreign-keys related
* to non-root tables within a joined-hierarchy.
*
* It tries to get to the bottom of the failures in
* {@link ForeignKeyConstraintTest#testGet}
*/
@TestForIssue( jiraKey = "HHH-11180" )
@DomainModel(
annotatedClasses = {
NonRootTablePolymorphicTests.Root.class,
NonRootTablePolymorphicTests.Sub.class,
NonRootTablePolymorphicTests.Leaf.class,
NonRootTablePolymorphicTests.SubParent.class,
NonRootTablePolymorphicTests.SubChild.class,
NonRootTablePolymorphicTests.SubGroup.class,
}
)
@SessionFactory
public class NonRootTablePolymorphicTests {
@Test
public void verifyBootModel(DomainModelScope scope) {
// check the foreign-keys within the joined-hierarchy
scope.withHierarchy(
Root.class,
(root) -> {
final org.hibernate.mapping.Table rootRootTable = root.getRootTable();
final org.hibernate.mapping.Table rootTable = root.getTable();
assertThat( rootRootTable, sameInstance( rootTable ) );
assertThat( rootRootTable.getName(), is( "root" ) );
final Iterator<Subclass> subclassIterator = root.getSubclassIterator();
while ( subclassIterator.hasNext() ) {
final JoinedSubclass subclass = (JoinedSubclass) subclassIterator.next();
final org.hibernate.mapping.Table subclassTable = subclass.getTable();
if ( subclass.getJpaEntityName().equals( "Sub" ) ) {
assertThat( subclass.getRootTable(), sameInstance( rootTable ) );
assertThat( subclassTable, not( sameInstance( rootTable ) ) );
assertThat( subclassTable.getName(), is( "sub" ) );
final PrimaryKey primaryKey = subclassTable.getPrimaryKey();
assertThat( primaryKey.getColumnSpan(), is( 1 ) );
assertThat( primaryKey.getColumn( 0 ).getName(), is( "sub_id" ) );
// the sub table should have 2 foreign-keys defined:
// 1) for the sub->root inheritance fk
// 2) for the sub->child fk
assertThat( subclassTable.getForeignKeys().size(), is( 2 ) );
subclassTable.getForeignKeyIterator().forEachRemaining(
(foreignKey) -> {
assertThat( foreignKey.getTable(), sameInstance( subclassTable ) );
// see which we have...
if ( foreignKey.getReferencedTable().getName().equals( "root" ) ) {
assertThat( foreignKey.getReferencedTable(), sameInstance( rootTable ) );
assertThat( foreignKey.getColumns().get( 0 ).getName(), is( "sub_id" ) );
// this is how the boot model represents an fk pointing to the pk..
assertThat(
foreignKey.getReferencedColumns(),
anyOf(
nullValue(),
CollectionMatchers.isEmpty()
)
);
}
else if ( foreignKey.getReferencedTable().getName().equals( "sub_child" ) ) {
assertThat( foreignKey.getColumns().get( 0 ).getName(), is( "child_fk" ) );
// this is how the boot model represents an fk pointing to the pk..
assertThat(
foreignKey.getReferencedColumns(),
anyOf(
nullValue(),
CollectionMatchers.isEmpty()
)
);
}
else {
fail( "Unexpected fk reference from `sub` to `" + foreignKey.getReferencedTable().getName() + "`" );
}
}
);
}
else if ( subclass.getJpaEntityName().equals( "Leaf" ) ) {
assertThat( subclass.getRootTable(), sameInstance( rootTable ) );
assertThat( subclassTable, not( sameInstance( rootTable ) ) );
assertThat( subclassTable.getName(), is( "leaf" ) );
final PrimaryKey primaryKey = subclassTable.getPrimaryKey();
assertThat( primaryKey.getColumnSpan(), is( 1 ) );
assertThat( primaryKey.getColumn( 0 ).getName(), is( "leaf_id" ) );
}
}
}
);
// check the association *to* the joined-hierarchy (aka, SubParent#sub)
// - we already checked the association *from* earlier
scope.withHierarchy(
SubParent.class,
(subParent) -> {
final Property subProperty = subParent.getProperty( "sub" );
final ToOne toOne = (ToOne) subProperty.getValue();
assertThat( toOne.getTable().getName(), is( "sub_parent" ) );
assertThat( toOne.getColumnSpan(), is( 1 ) );
final Selectable selectable = toOne.getColumnIterator().next();
assertThat( selectable.getText(), is( "parent_sub_fk" ) );
assertThat( subParent.getTable().getForeignKeys().size(), is( 1 ) );
final ForeignKey foreignKey = subParent.getTable().getForeignKeyIterator().next();
assertThat( foreignKey.getReferencedTable().getName(), is( "sub" ) );
assertThat( foreignKey.getTable(), sameInstance( toOne.getTable() ) );
}
);
}
@Test
public void verifyRuntimeModel(SessionFactoryScope scope) {
final EntityMappingType rootEntity = scope.getSessionFactory()
.getRuntimeMetamodels()
.getEntityMappingType( Root.class );
final EntityMappingType subEntity = scope.getSessionFactory()
.getRuntimeMetamodels()
.getEntityMappingType( Sub.class );
final EntityMappingType leafEntity = scope.getSessionFactory()
.getRuntimeMetamodels()
.getEntityMappingType( Leaf.class );
final EntityMappingType childEntity = scope.getSessionFactory()
.getRuntimeMetamodels()
.getEntityMappingType( SubChild.class );
final EntityMappingType parentEntity = scope.getSessionFactory()
.getRuntimeMetamodels()
.getEntityMappingType( SubParent.class );
// check Sub#child fk
final ToOneAttributeMapping childAttribute = (ToOneAttributeMapping) subEntity.findAttributeMapping( "child" );
final SimpleForeignKeyDescriptor childFk = (SimpleForeignKeyDescriptor) childAttribute.getForeignKeyDescriptor();
assertThat( childFk.getKeyTable(), is( "sub" ) );
assertThat( childFk.getTargetTable(), is( "sub_child" ) );
assertThat( childFk.getJdbcTypeCount(), is( 1 ) );
assertThat( childFk.getKeySide().getSelectableMapping().getSelectionExpression(), is( "child_fk" ) );
assertThat( childFk.getTargetSide().getSelectableMapping().getSelectionExpression(), is( "child_id" ) );
// check Parent#sub fk
final ToOneAttributeMapping subAttribute = (ToOneAttributeMapping) parentEntity.findAttributeMapping( "sub" );
final SimpleForeignKeyDescriptor subFk = (SimpleForeignKeyDescriptor) subAttribute.getForeignKeyDescriptor();
assertThat( subFk.getKeyTable(), is( "sub_parent" ) );
assertThat( subFk.getTargetTable(), is( "sub" ) );
assertThat( subFk.getJdbcTypeCount(), is( 1 ) );
assertThat( subFk.getKeySide().getSelectableMapping().getSelectionExpression(), is( "parent_sub_fk" ) );
assertThat( subFk.getTargetSide().getSelectableMapping().getSelectionExpression(), is( "sub_id" ) );
scope.inTransaction(
(session) -> {
session.createQuery( "from SubParent p join fetch p.sub" ).list();
session.createQuery( "from SubGroup p join fetch p.manyToManySubs" ).list();
session.createQuery( "from SubGroup p join fetch p.oneToManySubs" ).list();
}
);
// for sure the inheritance keys are messed up in the mapping model
}
@Entity( name = "Root" )
@Table( name = "root" )
@Inheritance( strategy = InheritanceType.JOINED )
public static class Root {
@Id
@javax.persistence.Column( name = "root_id" )
private Integer id;
private String name;
}
@Entity( name = "Sub" )
@Table( name = "sub" )
@PrimaryKeyJoinColumn( name = "sub_id", referencedColumnName = "root_id" )
public static class Sub extends Root {
private String subText;
@ManyToOne
@JoinColumn( name = "child_fk", referencedColumnName = "child_id" )
public SubChild child;
}
@Entity( name = "Leaf" )
@Table( name = "leaf" )
@PrimaryKeyJoinColumn( name = "leaf_id", referencedColumnName = "root_id" )
public static class Leaf extends Root {
private String teaLeaves;
}
@Entity( name = "SubParent" )
@Table( name = "sub_parent" )
public static class SubParent {
@Id
@javax.persistence.Column( name = "parent_id" )
private Integer id;
private String name;
@ManyToOne
@JoinColumn( name = "parent_sub_fk", referencedColumnName = "sub_id" )
public Sub sub;
}
@Entity( name = "SubChild" )
@Table( name = "sub_child" )
public static class SubChild {
@Id
@javax.persistence.Column( name = "child_id" )
private Integer id;
private String name;
}
@Entity( name = "SubGroup" )
@Table( name = "sub_group" )
public static class SubGroup {
@Id
@javax.persistence.Column( name = "group_id" )
private Integer id;
private String name;
@ManyToMany
@JoinTable(
name = "m2m_group",
joinColumns = @JoinColumn( name = "m2m_group_fk", referencedColumnName = "group_id" ),
inverseJoinColumns = @JoinColumn( name = "m2m_sub_fk", referencedColumnName = "sub_id" )
)
private List<Sub> manyToManySubs;
@OneToMany
@PrimaryKeyJoinColumn( name = "m_group_fk", referencedColumnName = "group_id" )
private List<Sub> oneToManySubs;
}
}

View File

@ -56,7 +56,7 @@ public class ManyToOneJoinTableTest {
final ToOneAttributeMapping simpleAttributeMapping = (ToOneAttributeMapping) simpleEntityAssociation; final ToOneAttributeMapping simpleAttributeMapping = (ToOneAttributeMapping) simpleEntityAssociation;
ForeignKeyDescriptor foreignKeyDescriptor = simpleAttributeMapping.getForeignKeyDescriptor(); ForeignKeyDescriptor foreignKeyDescriptor = simpleAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "other_simple" ) ); assertThat( selection.getContainingTableExpression(), is( "other_simple" ) );
assertThat( selection.getSelectionExpression(), is( "RHS_ID" ) ); assertThat( selection.getSelectionExpression(), is( "RHS_ID" ) );
@ -77,7 +77,7 @@ public class ManyToOneJoinTableTest {
final ToOneAttributeMapping anotherAttributeMapping = (ToOneAttributeMapping) anotherEntityAssociation; final ToOneAttributeMapping anotherAttributeMapping = (ToOneAttributeMapping) anotherEntityAssociation;
foreignKeyDescriptor = anotherAttributeMapping.getForeignKeyDescriptor(); foreignKeyDescriptor = anotherAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "other_another" ) ); assertThat( selection.getContainingTableExpression(), is( "other_another" ) );
assertThat( selection.getSelectionExpression(), is( "RHS_ID" ) ); assertThat( selection.getSelectionExpression(), is( "RHS_ID" ) );
@ -103,7 +103,7 @@ public class ManyToOneJoinTableTest {
ToOneAttributeMapping otherAttributeMapping = (ToOneAttributeMapping) otherEntityEntityAssociation; ToOneAttributeMapping otherAttributeMapping = (ToOneAttributeMapping) otherEntityEntityAssociation;
foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor(); foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "other_simple" ) ); assertThat( selection.getContainingTableExpression(), is( "other_simple" ) );
assertThat( selection.getSelectionExpression(), is( "LHS_ID" ) ); assertThat( selection.getSelectionExpression(), is( "LHS_ID" ) );
@ -129,7 +129,7 @@ public class ManyToOneJoinTableTest {
otherAttributeMapping = (ToOneAttributeMapping) otherEntityEntityAssociation; otherAttributeMapping = (ToOneAttributeMapping) otherEntityEntityAssociation;
foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor(); foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "another_entity" ) ); assertThat( selection.getContainingTableExpression(), is( "another_entity" ) );
assertThat( selection.getSelectionExpression(), is( "other_id" ) ); assertThat( selection.getSelectionExpression(), is( "other_id" ) );

View File

@ -53,7 +53,7 @@ public class ManyToOneTest {
final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) simpleEntityAssociation; final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) simpleEntityAssociation;
ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor(); ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "other_entity" ) ); assertThat( selection.getContainingTableExpression(), is( "other_entity" ) );
assertThat( selection.getSelectionExpression(), is( "simple_entity_id" ) ); assertThat( selection.getSelectionExpression(), is( "simple_entity_id" ) );

View File

@ -51,7 +51,7 @@ public class EntityWithBidirectionalAssociationTest {
final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) childAssociation; final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) childAssociation;
ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor(); ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "PARENT" ) ); assertThat( selection.getContainingTableExpression(), is( "PARENT" ) );
assertThat( selection.getSelectionExpression(), is( "child_id" ) ); assertThat( selection.getSelectionExpression(), is( "child_id" ) );
@ -76,7 +76,7 @@ public class EntityWithBidirectionalAssociationTest {
final ToOneAttributeMapping parentAttributeMapping = (ToOneAttributeMapping) parentAssociation; final ToOneAttributeMapping parentAttributeMapping = (ToOneAttributeMapping) parentAssociation;
foreignKeyDescriptor = parentAttributeMapping.getForeignKeyDescriptor(); foreignKeyDescriptor = parentAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "PARENT" ) ); assertThat( selection.getContainingTableExpression(), is( "PARENT" ) );
assertThat( selection.getSelectionExpression(), is( "child_id" ) ); assertThat( selection.getSelectionExpression(), is( "child_id" ) );

View File

@ -53,7 +53,7 @@ public class EntityWithOneBidirectionalJoinTableAssociationTest {
final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) childAssociation; final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) childAssociation;
ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor(); ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "PARENT_CHILD" ) ); assertThat( selection.getContainingTableExpression(), is( "PARENT_CHILD" ) );
assertThat( selection.getSelectionExpression(), is( "child_id" ) ); assertThat( selection.getSelectionExpression(), is( "child_id" ) );
@ -78,7 +78,7 @@ public class EntityWithOneBidirectionalJoinTableAssociationTest {
final ToOneAttributeMapping parentAttributeMapping = (ToOneAttributeMapping) parentAssociation; final ToOneAttributeMapping parentAttributeMapping = (ToOneAttributeMapping) parentAssociation;
foreignKeyDescriptor = parentAttributeMapping.getForeignKeyDescriptor(); foreignKeyDescriptor = parentAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "PARENT_CHILD" ) ); assertThat( selection.getContainingTableExpression(), is( "PARENT_CHILD" ) );
assertThat( selection.getSelectionExpression(), is( "parent_id" ) ); assertThat( selection.getSelectionExpression(), is( "parent_id" ) );

View File

@ -47,7 +47,7 @@ public class EntityWithOneToOneJoinTableTest {
final ToOneAttributeMapping otherAttributeMapping = (ToOneAttributeMapping) other; final ToOneAttributeMapping otherAttributeMapping = (ToOneAttributeMapping) other;
final ForeignKeyDescriptor foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor(); final ForeignKeyDescriptor foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "Entity_SimpleEntity" ) ); assertThat( selection.getContainingTableExpression(), is( "Entity_SimpleEntity" ) );
assertThat( selection.getSelectionExpression(), is( "other_id" ) ); assertThat( selection.getSelectionExpression(), is( "other_id" ) );

View File

@ -48,7 +48,7 @@ public class EntityWithOneToOneSharingPrimaryKeyTest {
final ToOneAttributeMapping otherAttributeMapping = (ToOneAttributeMapping) otherAssociation; final ToOneAttributeMapping otherAttributeMapping = (ToOneAttributeMapping) otherAssociation;
ForeignKeyDescriptor foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor(); ForeignKeyDescriptor foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor();
foreignKeyDescriptor.visitReferringSelectables( foreignKeyDescriptor.visitKeySelectables(
(columnIndex, selection) -> { (columnIndex, selection) -> {
assertThat( selection.getContainingTableExpression(), is( "EntityWithOneToOneSharingPrimaryKey" ) ); assertThat( selection.getContainingTableExpression(), is( "EntityWithOneToOneSharingPrimaryKey" ) );
assertThat( selection.getSelectionExpression(), is( "id" ) ); assertThat( selection.getSelectionExpression(), is( "id" ) );