From 849246e3cd75e10733c475e18b684ee61daba6dc Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 31 Oct 2022 15:41:17 +0100 Subject: [PATCH] finally move propertyName from AnnotatedColumn --- .../org/hibernate/cfg/AnnotatedColumn.java | 96 +++---------------- .../org/hibernate/cfg/AnnotatedColumns.java | 64 +++++++++++-- .../hibernate/cfg/AnnotatedJoinColumn.java | 22 ++--- .../hibernate/cfg/AnnotatedJoinColumns.java | 17 +++- .../org/hibernate/cfg/AnnotationBinder.java | 4 +- .../java/org/hibernate/cfg/BinderHelper.java | 3 +- .../org/hibernate/cfg/ColumnsBuilder.java | 21 ++-- .../cfg/IndexOrUniqueKeySecondPass.java | 15 ++- .../java/org/hibernate/cfg/ToOneBinder.java | 5 +- .../cfg/annotations/BasicValueBinder.java | 8 +- .../cfg/annotations/CollectionBinder.java | 19 ++-- 11 files changed, 123 insertions(+), 151 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedColumn.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedColumn.java index 7bf84d1fc4..7b192fc534 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedColumn.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedColumn.java @@ -6,7 +6,6 @@ */ package org.hibernate.cfg; -import java.util.List; import java.util.Map; import org.hibernate.AnnotationException; @@ -44,7 +43,6 @@ import static org.hibernate.cfg.BinderHelper.isEmptyAnnotationValue; import static org.hibernate.internal.util.StringHelper.isEmpty; import static org.hibernate.internal.util.StringHelper.isNotEmpty; import static org.hibernate.internal.util.StringHelper.nullIfEmpty; -import static org.hibernate.internal.util.StringHelper.qualify; /** * A mapping to a column, logically representing a @@ -75,7 +73,6 @@ public class AnnotatedColumn { private Integer precision; private Integer scale; private String logicalColumnName; - private String propertyName; // this is really a .-separated property path private boolean unique; private boolean nullable = true; private String formulaString; @@ -180,17 +177,6 @@ public class AnnotatedColumn { this.logicalColumnName = logicalColumnName; } - public void setPropertyName(String propertyName) { - this.propertyName = propertyName; - } - - /** - * A property path relative to the {@link #getPropertyHolder() PropertyHolder}. - */ - public String getPropertyName() { - return propertyName; - } - public void setUnique(boolean unique) { this.unique = unique; } @@ -243,7 +229,7 @@ public class AnnotatedColumn { else { initMappingColumn( logicalColumnName, - propertyName, + getParent().getPropertyName(), length, precision, scale, @@ -361,8 +347,8 @@ public class AnnotatedColumn { public boolean isCollectionElement() { // if the propertyHolder is a collection, assume the // @Column refers to the element column - return !getPropertyHolder().isComponent() - && !getPropertyHolder().isEntity(); + final PropertyHolder propertyHolder = getParent().getPropertyHolder(); + return !propertyHolder.isComponent() && !propertyHolder.isEntity(); } @Override @@ -409,10 +395,6 @@ public class AnnotatedColumn { } } - public PropertyHolder getPropertyHolder() { - return getParent().getPropertyHolder(); - } - protected void setMappingColumn(Column mappingColumn) { this.mappingColumn = mappingColumn; } @@ -450,7 +432,7 @@ public class AnnotatedColumn { new ImplicitBasicColumnNameSource() { @Override public AttributePath getAttributePath() { - return AttributePath.parse( propertyName ); + return AttributePath.parse( getParent().getPropertyName() ); } @Override @@ -470,27 +452,6 @@ public class AnnotatedColumn { getBuildingContext().getMetadataCollector().addColumnNameBinding( value.getTable(), logicalColumnName, getMappingColumn() ); } - /** - * Find appropriate table of the column. - * It can come from a secondary table or from the main table of the persistent class - * - * @return appropriate table - * @throws AnnotationException missing secondary table - */ - public Table getTable() { - return getParent().getTable(); - } - - //TODO: move to AnnotatedColumns - public boolean isSecondary() { - return getParent().isSecondary(); - } - - //TODO: move to AnnotatedColumns - public Join getJoin() { - return getParent().getJoin(); - } - public void forceNotNull() { if ( mappingColumn == null ) { throw new CannotForceNonNullableException( @@ -639,6 +600,7 @@ public class AnnotatedColumn { if ( formulaAnn != null ) { final AnnotatedColumns parent = new AnnotatedColumns(); parent.setPropertyHolder( propertyHolder ); + parent.setPropertyName( getRelativePath( propertyHolder, inferredData.getPropertyName() ) ); parent.setBuildingContext( context ); parent.setJoins( secondaryTables ); //unnecessary final AnnotatedColumn formulaColumn = new AnnotatedColumn(); @@ -681,15 +643,14 @@ public class AnnotatedColumn { jakarta.persistence.Column[] columns, PropertyHolder propertyHolder, PropertyData inferredData ) { - final jakarta.persistence.Column[] overriddenCols = propertyHolder.getOverriddenColumn( - qualify( propertyHolder.getPath(), inferredData.getPropertyName() ) - ); + final String path = getPath( propertyHolder, inferredData ); + final jakarta.persistence.Column[] overriddenCols = propertyHolder.getOverriddenColumn( path ); if ( overriddenCols != null ) { //check for overridden first if ( columns != null && overriddenCols.length != columns.length ) { //TODO: unfortunately, we never actually see this nice error message, since // PersistentClass.validate() gets called first and produces a worse message - throw new AnnotationException( "Property '" + getPath( propertyHolder, inferredData ) + throw new AnnotationException( "Property '" + path + "' specifies " + columns.length + " '@AttributeOverride's but the overridden property has " + overriddenCols.length + " columns (every column must have exactly one '@AttributeOverride')" ); @@ -712,6 +673,7 @@ public class AnnotatedColumn { jakarta.persistence.Column[] actualCols) { final AnnotatedColumns parent = new AnnotatedColumns(); parent.setPropertyHolder( propertyHolder ); + parent.setPropertyName( getRelativePath( propertyHolder, inferredData.getPropertyName() ) ); parent.setJoins( secondaryTables ); parent.setBuildingContext( context ); for ( jakarta.persistence.Column column : actualCols ) { @@ -760,7 +722,7 @@ public class AnnotatedColumn { jakarta.persistence.Column column, String sqlType, String tableName) { - final String columnName = getLogicalColumnName( inferredData, suffixForDefaultColumnName, database, column ); + final String columnName = logicalColumnName( inferredData, suffixForDefaultColumnName, database, column ); final AnnotatedColumn annotatedColumn = new AnnotatedColumn(); annotatedColumn.setLogicalColumnName( columnName ); annotatedColumn.setImplicit( false ); @@ -769,7 +731,7 @@ public class AnnotatedColumn { annotatedColumn.setPrecision( column.precision() ); annotatedColumn.setScale( column.scale() ); // annotatedColumn.setPropertyHolder( propertyHolder ); - annotatedColumn.setPropertyName( getRelativePath( propertyHolder, inferredData.getPropertyName() ) ); +// annotatedColumn.setPropertyName( getRelativePath( propertyHolder, inferredData.getPropertyName() ) ); annotatedColumn.setNullable( column.nullable() ); //TODO force to not null if available? This is a (bad) user choice. if ( comment != null ) { annotatedColumn.setComment( comment.value() ); @@ -789,7 +751,7 @@ public class AnnotatedColumn { return annotatedColumn; } - private static String getLogicalColumnName( + private static String logicalColumnName( PropertyData inferredData, String suffixForDefaultColumnName, Database database, @@ -896,6 +858,7 @@ public class AnnotatedColumn { MetadataBuildingContext context) { final AnnotatedColumns columns = new AnnotatedColumns(); columns.setPropertyHolder( propertyHolder ); + columns.setPropertyName( getRelativePath( propertyHolder, inferredData.getPropertyName() ) ); columns.setBuildingContext( context ); columns.setJoins( secondaryTables ); columns.setPropertyHolder( propertyHolder ); @@ -911,7 +874,7 @@ public class AnnotatedColumn { } final String propertyName = inferredData.getPropertyName(); // column.setPropertyHolder( propertyHolder ); - column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); +// column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); // column.setJoins( secondaryTables ); // column.setBuildingContext( context ); // property name + suffix is an "explicit" column name @@ -929,37 +892,6 @@ public class AnnotatedColumn { return columns; } - public static void checkPropertyConsistency(List columns, String propertyName) { - if ( columns.size() > 1 ) { - for ( int currentIndex = 1; currentIndex < columns.size(); currentIndex++ ) { - final AnnotatedColumn current = columns.get( currentIndex ); - final AnnotatedColumn previous = columns.get( currentIndex - 1 ); - if ( !current.isFormula() && !previous.isFormula() ) { - if ( current.isNullable() != previous.isNullable() ) { - throw new AnnotationException( - "Column mappings for property '" + propertyName + "' mix nullable with 'not null'" - ); - } - if ( current.isInsertable() != previous.isInsertable() ) { - throw new AnnotationException( - "Column mappings for property '" + propertyName + "' mix insertable with 'insertable=false'" - ); - } - if ( current.isUpdatable() != previous.isUpdatable() ) { - throw new AnnotationException( - "Column mappings for property '" + propertyName + "' mix updatable with 'updatable=false'" - ); - } - if ( !current.getExplicitTableName().equals( previous.getExplicitTableName() ) ) { - throw new AnnotationException( - "Column mappings for property '" + propertyName + "' mix distinct secondary tables" - ); - } - } - } - } - } - public void addIndex(Index index, boolean inSecondPass) { if ( index != null ) { addIndex( index.name(), inSecondPass ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedColumns.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedColumns.java index 60956d9a03..537d81b511 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedColumns.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedColumns.java @@ -1,7 +1,6 @@ package org.hibernate.cfg; import org.hibernate.AnnotationException; -import org.hibernate.AssertionFailure; import org.hibernate.boot.spi.MetadataBuildingContext; import org.hibernate.mapping.Join; import org.hibernate.mapping.Table; @@ -32,6 +31,7 @@ public class AnnotatedColumns { private final List columns = new ArrayList<>(); private Table table; private PropertyHolder propertyHolder; + private String propertyName; // this is really a .-separated property path private Map joins = Collections.emptyMap(); private MetadataBuildingContext buildingContext; @@ -47,6 +47,17 @@ public class AnnotatedColumns { this.propertyHolder = propertyHolder; } + /** + * A property path relative to the {@link #getPropertyHolder() PropertyHolder}. + */ + public String getPropertyName() { + return propertyName; + } + + public void setPropertyName(String propertyName) { + this.propertyName = propertyName; + } + public void setBuildingContext(MetadataBuildingContext buildingContext) { this.buildingContext = buildingContext; } @@ -62,6 +73,7 @@ public class AnnotatedColumns { public Join getJoin() { final AnnotatedColumn firstColumn = columns.get(0); final String explicitTableName = firstColumn.getExplicitTableName(); + //note: checkPropertyConsistency() is responsible for ensuring they all have the same table name Join join = joins.get( explicitTableName ); if ( join == null ) { // annotation binding seems to use logical and physical naming somewhat inconsistently... @@ -77,19 +89,27 @@ public class AnnotatedColumns { + "' is not declared (use '@SecondaryTable' to declare the secondary table)" ); } - return join; + else { + return join; + } } public boolean isSecondary() { - if ( getPropertyHolder() == null ) { - throw new AssertionFailure( "Should not call isSecondary() on column w/o persistent class defined" ); - } final AnnotatedColumn firstColumn = columns.get(0); final String explicitTableName = firstColumn.getExplicitTableName(); + //note: checkPropertyConsistency() is responsible for ensuring they all have the same table name return isNotEmpty( explicitTableName ) && !getPropertyHolder().getTable().getName().equals( explicitTableName ); } + /** + * Find the table to which these columns belong, taking into account secondary tables. + * + * @return the {@link Table} given in the code by {@link jakarta.persistence.Column#table()} + * and held here by {@code explicitTableName}. + * @throws AnnotationException if the table named by {@code explicitTableName} is not found + * among the secondary tables in {@code joins}. + */ public Table getTable() { if ( table != null ) { return table; @@ -98,8 +118,7 @@ public class AnnotatedColumns { // all the columns have to be mapped to the same table // even though at the annotation level it looks like // they could each specify a different table - final AnnotatedColumn firstColumn = columns.get(0); - return firstColumn.isSecondary() ? getJoin().getTable() : getPropertyHolder().getTable(); + return isSecondary() ? getJoin().getTable() : getPropertyHolder().getTable(); } } @@ -115,4 +134,35 @@ public class AnnotatedColumns { public void addColumn(AnnotatedColumn child) { columns.add( child ); } + + public void checkPropertyConsistency() { + if ( columns.size() > 1 ) { + for ( int currentIndex = 1; currentIndex < columns.size(); currentIndex++ ) { + final AnnotatedColumn current = columns.get( currentIndex ); + final AnnotatedColumn previous = columns.get( currentIndex - 1 ); + if ( !current.isFormula() && !previous.isFormula() ) { + if ( current.isNullable() != previous.isNullable() ) { + throw new AnnotationException( + "Column mappings for property '" + propertyName + "' mix nullable with 'not null'" + ); + } + if ( current.isInsertable() != previous.isInsertable() ) { + throw new AnnotationException( + "Column mappings for property '" + propertyName + "' mix insertable with 'insertable=false'" + ); + } + if ( current.isUpdatable() != previous.isUpdatable() ) { + throw new AnnotationException( + "Column mappings for property '" + propertyName + "' mix updatable with 'updatable=false'" + ); + } + if ( !current.getExplicitTableName().equals( previous.getExplicitTableName() ) ) { + throw new AnnotationException( + "Column mappings for property '" + propertyName + "' mix distinct secondary tables" + ); + } + } + } + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedJoinColumn.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedJoinColumn.java index f2b6013f21..643fc421cc 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedJoinColumn.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedJoinColumn.java @@ -95,15 +95,13 @@ public class AnnotatedJoinColumn extends AnnotatedColumn { public static AnnotatedJoinColumn buildJoinFormula( JoinFormula joinFormula, - AnnotatedJoinColumns parent, - PropertyHolder propertyHolder, - String propertyName) { + AnnotatedJoinColumns parent) { final AnnotatedJoinColumn formulaColumn = new AnnotatedJoinColumn(); formulaColumn.setFormula( joinFormula.value() ); formulaColumn.setReferencedColumn( joinFormula.referencedColumnName() ); // formulaColumn.setContext( buildingContext ); // formulaColumn.setPropertyHolder( propertyHolder ); - formulaColumn.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); +// formulaColumn.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); // formulaColumn.setJoins( joins ); formulaColumn.setParent( parent ); formulaColumn.bind(); @@ -125,10 +123,10 @@ public class AnnotatedJoinColumn extends AnnotatedColumn { + "' is 'mappedBy' a different entity and may not explicitly specify the '@JoinColumn'" ); } - return explicitJoinColumn( joinColumn, comment, parent, propertyHolder, propertyName, defaultColumnSuffix ); + return explicitJoinColumn( joinColumn, comment, parent, propertyName, defaultColumnSuffix ); } else { - return implicitJoinColumn( parent, propertyHolder, propertyName, defaultColumnSuffix ); + return implicitJoinColumn( parent, propertyName, defaultColumnSuffix ); } } @@ -136,7 +134,6 @@ public class AnnotatedJoinColumn extends AnnotatedColumn { JoinColumn joinColumn, Comment comment, AnnotatedJoinColumns parent, - PropertyHolder propertyHolder, String propertyName, String defaultColumnSuffix) { final AnnotatedJoinColumn column = new AnnotatedJoinColumn(); @@ -147,7 +144,7 @@ public class AnnotatedJoinColumn extends AnnotatedColumn { if ( isEmpty( column.getLogicalColumnName() ) && isNotEmpty( defaultColumnSuffix ) ) { column.setLogicalColumnName( propertyName + defaultColumnSuffix ); } - column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); +// column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); column.setImplicit( false ); column.setParent( parent ); column.applyJoinAnnotation( joinColumn, null ); @@ -157,14 +154,13 @@ public class AnnotatedJoinColumn extends AnnotatedColumn { private static AnnotatedJoinColumn implicitJoinColumn( AnnotatedJoinColumns parent, - PropertyHolder propertyHolder, String propertyName, String defaultColumnSuffix) { final AnnotatedJoinColumn column = new AnnotatedJoinColumn(); // column.setContext( context ); // column.setJoins( joins ); // column.setPropertyHolder( propertyHolder ); - column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); +// column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); // property name + suffix is an "explicit" column name if ( isNotEmpty( defaultColumnSuffix ) ) { column.setLogicalColumnName( propertyName + defaultColumnSuffix ); @@ -370,7 +366,7 @@ public class AnnotatedJoinColumn extends AnnotatedColumn { final String unquotedRefColumn = unquote( referencedColumn ); final String collectionColName = isNotEmpty( unquotedLogColName ) ? unquotedLogColName - : getPropertyName() + '_' + unquotedRefColumn; + : getParent().getPropertyName() + '_' + unquotedRefColumn; final InFlightMetadataCollector collector = getBuildingContext().getMetadataCollector(); final String logicalCollectionColumnName = collector.getDatabase() .getJdbcEnvironment() @@ -490,7 +486,7 @@ public class AnnotatedJoinColumn extends AnnotatedColumn { column.setImplicit( true ); column.setNullable( false ); //I break the spec, but it's for good // column.setPropertyHolder( propertyHolder ); - column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); +// column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); // column.setJoins( secondaryTables ); // column.setContext( context ); column.setParent( parent ); @@ -506,7 +502,7 @@ public class AnnotatedJoinColumn extends AnnotatedColumn { final AnnotatedJoinColumn column = new AnnotatedJoinColumn(); column.setImplicit( true ); // column.setPropertyHolder( propertyHolder ); - column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); +// column.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); // column.setJoins( secondaryTables ); // column.setContext( context ); column.setNullable( false ); //I break the spec, but it's for good diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedJoinColumns.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedJoinColumns.java index 3e81fefeb6..f68e0d6855 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedJoinColumns.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotatedJoinColumns.java @@ -78,7 +78,7 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { final JoinFormula formula = columnOrFormula.formula(); final JoinColumn column = columnOrFormula.column(); if ( !isEmptyOrNullAnnotationValue( formula.value() ) ) { - AnnotatedJoinColumn.buildJoinFormula( formula, parent, propertyHolder, propertyName ); + AnnotatedJoinColumn.buildJoinFormula( formula, parent ); } else { AnnotatedJoinColumn.buildJoinColumn( column, mappedBy, parent, propertyHolder, propertyName ); @@ -87,6 +87,21 @@ public class AnnotatedJoinColumns extends AnnotatedColumns { return parent; } + static AnnotatedJoinColumns buildJoinColumnsWithFormula( + String propertyName, + JoinFormula joinFormula, + Map secondaryTables, + PropertyHolder propertyHolder, + MetadataBuildingContext context) { + final AnnotatedJoinColumns joinColumns = new AnnotatedJoinColumns(); + joinColumns.setBuildingContext( context ); + joinColumns.setJoins( secondaryTables ); + joinColumns.setPropertyHolder( propertyHolder ); + joinColumns.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); + AnnotatedJoinColumn.buildJoinFormula( joinFormula, joinColumns ); + return joinColumns; + } + public static AnnotatedJoinColumns buildJoinColumns( JoinColumn[] joinColumns, Comment comment, diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 4e4a3e41b5..0ba9f9a59e 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -1640,14 +1640,14 @@ public final class AnnotationBinder { final NaturalId naturalIdAnn = property.getAnnotation( NaturalId.class ); if ( naturalIdAnn != null ) { if ( joinColumns != null ) { + String keyName = "UK_" + Constraint.hashedName( joinColumns.getTable().getName() + "_NaturalID" ); for ( AnnotatedColumn column : joinColumns.getColumns() ) { - String keyName = "UK_" + Constraint.hashedName( column.getTable().getName() + "_NaturalID" ); column.addUniqueKey( keyName, inSecondPass ); } } else { + String keyName = "UK_" + Constraint.hashedName( columns.getTable().getName() + "_NaturalID" ); for ( AnnotatedColumn column : columns.getColumns() ) { - String keyName = "UK_" + Constraint.hashedName( column.getTable().getName() + "_NaturalID" ); column.addUniqueKey( keyName, inSecondPass ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java index ab3918c4cf..e0cdd5c8df 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java @@ -1111,8 +1111,7 @@ public class BinderHelper { final BasicValue keyDescriptor = keyValueBinder.make(); value.setKey( keyDescriptor ); keyValueBinder.fillSimpleValue(); - final String path = qualify( propertyHolder.getEntityName(), inferredData.getPropertyName() ); - AnnotatedColumn.checkPropertyConsistency( keyColumns.getColumns(), path ); + keyColumns.checkPropertyConsistency(); columns.get(0).linkWithValue( keyDescriptor ); //TODO: nasty return value; } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/ColumnsBuilder.java b/hibernate-core/src/main/java/org/hibernate/cfg/ColumnsBuilder.java index f18defb8b0..5cddcd9ea2 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/ColumnsBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/ColumnsBuilder.java @@ -5,6 +5,7 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.cfg; + import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; import jakarta.persistence.JoinColumn; @@ -34,7 +35,6 @@ import static org.hibernate.cfg.AnnotatedColumn.buildFormulaFromAnnotation; import static org.hibernate.cfg.BinderHelper.getOverridableAnnotation; import static org.hibernate.cfg.BinderHelper.getPath; import static org.hibernate.cfg.BinderHelper.getPropertyOverriddenByMapperOrMapsId; -import static org.hibernate.cfg.BinderHelper.getRelativePath; import static org.hibernate.internal.util.StringHelper.isEmpty; /** @@ -202,7 +202,6 @@ class ColumnsBuilder { private AnnotatedJoinColumns buildExplicitJoinColumns(XProperty property, PropertyData inferredData) { // process @JoinColumns before @Columns to handle collection of entities properly - final String propertyName = inferredData.getPropertyName(); final JoinColumn[] joinColumnAnnotations = getJoinColumnAnnotations( property, inferredData ); @@ -232,22 +231,18 @@ class ColumnsBuilder { if ( property.isAnnotationPresent( JoinFormula.class) ) { final JoinFormula joinFormula = getOverridableAnnotation( property, JoinFormula.class, buildingContext ); - return buildJoinColumnsWithFormula( propertyName, joinFormula ); + return AnnotatedJoinColumns.buildJoinColumnsWithFormula( + propertyName, + joinFormula, + entityBinder.getSecondaryTables(), + propertyHolder, + buildingContext + ); } return null; } - private AnnotatedJoinColumns buildJoinColumnsWithFormula(String propertyName, JoinFormula joinFormula) { - final AnnotatedJoinColumns joinColumns = new AnnotatedJoinColumns(); - joinColumns.setBuildingContext( buildingContext ); - joinColumns.setJoins( entityBinder.getSecondaryTables() ); - joinColumns.setPropertyHolder( propertyHolder ); - joinColumns.setPropertyName( getRelativePath( propertyHolder, propertyName ) ); - AnnotatedJoinColumn.buildJoinFormula( joinFormula, joinColumns, propertyHolder, propertyName ); - return joinColumns; - } - private JoinColumnOrFormula[] joinColumnOrFormulaAnnotations(XProperty property, PropertyData inferredData) { if ( property.isAnnotationPresent( JoinColumnOrFormula.class ) ) { return new JoinColumnOrFormula[] { property.getAnnotation( JoinColumnOrFormula.class ) }; diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java b/hibernate-core/src/main/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java index af04274ef7..2b962b779c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java @@ -64,16 +64,13 @@ public class IndexOrUniqueKeySecondPass implements SecondPass { } } if ( column != null ) { - table = column.getTable(); - - final PropertyHolder propertyHolder = column.getPropertyHolder(); - final String entityName = ( propertyHolder.isComponent() ) ? - propertyHolder.getPersistentClass().getEntityName() : - propertyHolder.getEntityName(); - + table = column.getParent().getTable(); + final PropertyHolder propertyHolder = column.getParent().getPropertyHolder(); + final String entityName = propertyHolder.isComponent() + ? propertyHolder.getPersistentClass().getEntityName() + : propertyHolder.getEntityName(); final PersistentClass persistentClass = persistentClasses.get( entityName ); - final Property property = persistentClass.getProperty( column.getPropertyName() ); - + final Property property = persistentClass.getProperty( column.getParent().getPropertyName() ); if ( property.getValue() instanceof Component ) { final Component component = (Component) property.getValue(); final List columns = new ArrayList<>(); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/ToOneBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/ToOneBinder.java index dee2553783..fc8bf80175 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/ToOneBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/ToOneBinder.java @@ -45,7 +45,6 @@ import org.hibernate.mapping.ToOne; import java.util.ArrayList; import java.util.List; -import static org.hibernate.cfg.AnnotatedColumn.checkPropertyConsistency; import static org.hibernate.cfg.AnnotationBinder.matchIgnoreNotFoundWithFetchType; import static org.hibernate.cfg.BinderHelper.getCascadeStrategy; import static org.hibernate.cfg.BinderHelper.getFetchMode; @@ -196,7 +195,6 @@ public class ToOneBinder { cascadeStrategy, joinColumns, optional, - propertyHolder, inferredData, isIdentifierMapper, propertyBinder, @@ -242,7 +240,6 @@ public class ToOneBinder { String cascadeStrategy, AnnotatedJoinColumns columns, boolean optional, - PropertyHolder propertyHolder, PropertyData inferredData, boolean isIdentifierMapper, PropertyBinder propertyBinder, @@ -250,7 +247,7 @@ public class ToOneBinder { boolean hasSpecjManyToOne, String propertyName) { - checkPropertyConsistency( columns.getColumns(), qualify( propertyHolder.getEntityName(), propertyName ) ); + columns.checkPropertyConsistency(); //PropertyBinder binder = new PropertyBinder(); propertyBinder.setName( propertyName ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/BasicValueBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/BasicValueBinder.java index 86dc61eec9..40c4829dd8 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/BasicValueBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/BasicValueBinder.java @@ -1069,16 +1069,12 @@ public class BasicValueBinder implements JdbcTypeIndicators { this.explicitBasicTypeName = explicitType; } - private void validate() { - AnnotatedColumn.checkPropertyConsistency( columns.getColumns(), propertyName ); - } - public BasicValue make() { if ( basicValue != null ) { return basicValue; } - validate(); + columns.checkPropertyConsistency(); LOG.debugf( "building BasicValue for %s", propertyName ); @@ -1130,7 +1126,7 @@ public class BasicValueBinder implements JdbcTypeIndicators { final AnnotatedJoinColumns joinColumns = new AnnotatedJoinColumns(); joinColumns.setBuildingContext( buildingContext ); joinColumns.setPropertyHolder( columns.getPropertyHolder() ); - joinColumns.setPropertyName( firstColumn.getPropertyName() ); + joinColumns.setPropertyName( columns.getPropertyName() ); //TODO: resetting the parent here looks like a dangerous thing to do // should we be cloning them first (the legacy code did not) for ( AnnotatedColumn column : columns.getColumns() ) { diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index a12062bd45..5d91b57b64 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -154,7 +154,6 @@ import static org.hibernate.cfg.AnnotatedColumn.buildColumnFromAnnotation; import static org.hibernate.cfg.AnnotatedColumn.buildColumnFromNoAnnotation; import static org.hibernate.cfg.AnnotatedColumn.buildColumnsFromAnnotations; import static org.hibernate.cfg.AnnotatedColumn.buildFormulaFromAnnotation; -import static org.hibernate.cfg.AnnotatedColumn.checkPropertyConsistency; import static org.hibernate.cfg.AnnotatedJoinColumns.buildJoinColumnsWithDefaultColumnSuffix; import static org.hibernate.cfg.AnnotatedJoinColumns.buildJoinTableJoinColumns; import static org.hibernate.cfg.AnnotationBinder.fillComponent; @@ -417,11 +416,9 @@ public abstract class CollectionBinder { final String mappedBy; final ReflectionManager reflectionManager = context.getBootstrapContext().getReflectionManager(); if ( oneToManyAnn != null ) { - for ( AnnotatedJoinColumn column : joinColumns.getJoinColumns() ) { - if ( column.isSecondary() ) { - //TODO: fix the error message - throw new NotYetImplementedException( "Collections having FK in secondary table" ); - } + if ( joinColumns.isSecondary() ) { + //TODO: fix the error message + throw new NotYetImplementedException( "Collections having FK in secondary table" ); } collectionBinder.setFkJoinColumns( joinColumns ); mappedBy = oneToManyAnn.mappedBy(); @@ -433,11 +430,9 @@ public abstract class CollectionBinder { collectionBinder.setOneToMany( true ); } else if ( elementCollectionAnn != null ) { - for ( AnnotatedJoinColumn column : joinColumns.getJoinColumns() ) { - if ( column.isSecondary() ) { - //TODO: fix the error message - throw new NotYetImplementedException( "Collections having FK in secondary table" ); - } + if ( joinColumns.isSecondary() ) { + //TODO: fix the error message + throw new NotYetImplementedException( "Collections having FK in secondary table" ); } collectionBinder.setFkJoinColumns( joinColumns ); mappedBy = ""; @@ -1852,8 +1847,8 @@ public abstract class CollectionBinder { final DependantValue key = new DependantValue( buildingContext, collection.getCollectionTable(), keyValue ); key.setTypeName( null ); + joinColumns.checkPropertyConsistency(); final List columns = joinColumns.getColumns(); - checkPropertyConsistency( columns, collection.getOwnerEntityName() ); key.setNullable( columns.isEmpty() || columns.get(0).isNullable() ); key.setUpdateable( columns.isEmpty() || columns.get(0).isUpdatable() ); key.setCascadeDeleteEnabled( cascadeDeleteEnabled );