Skip to content

Commit 52fa8ac

Browse files
committed
some minor refactorings around Column
1 parent 189a07d commit 52fa8ac

File tree

6 files changed

+153
-135
lines changed

6 files changed

+153
-135
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AbstractPropertyHolder.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.List;
1010
import java.util.Map;
1111

12+
import jakarta.persistence.AttributeConverter;
1213
import org.hibernate.AnnotationException;
1314
import org.hibernate.AssertionFailure;
1415
import org.hibernate.annotations.ColumnTransformer;
@@ -111,11 +112,11 @@ public ConverterDescriptor<?,?> resolveAttributeConverterDescriptor(MemberDetail
111112
}
112113

113114
protected IllegalStateException buildExceptionFromInstantiationError(AttributeConversionInfo info, Exception e) {
114-
if ( void.class.equals( info.getConverterClass() ) ) {
115+
if ( AttributeConverter.class.equals( info.getConverterClass() ) ) {
115116
// the user forgot to set @Convert.converter
116117
// we already know it's not a @Convert.disableConversion
117118
return new IllegalStateException(
118-
"Unable to instantiate AttributeConverter: you left @Convert.converter to its default value void.",
119+
"Unable to instantiate AttributeConverter because the 'converter' member of '@Convert' was not specified",
119120
e
120121
);
121122

@@ -283,7 +284,7 @@ private Column[] getExactOverriddenColumn(String propertyName) {
283284
*/
284285
@Override
285286
public JoinColumn[] getOverriddenJoinColumn(String propertyName) {
286-
final JoinColumn[] result = getExactOverriddenJoinColumn( propertyName );
287+
final var result = getExactOverriddenJoinColumn( propertyName );
287288
if ( result == null && propertyName.contains( ".{element}." ) ) {
288289
//support for non map collections where no prefix is needed
289290
//TODO cache the underlying regexp
@@ -316,13 +317,15 @@ private JoinColumn[] getExactOverriddenJoinColumn(String propertyName) {
316317

317318
@Override
318319
public ForeignKey getOverriddenForeignKey(String propertyName) {
319-
final ForeignKey result = getExactOverriddenForeignKey( propertyName );
320+
final var result = getExactOverriddenForeignKey( propertyName );
320321
if ( result == null && propertyName.contains( ".{element}." ) ) {
321322
//support for non map collections where no prefix is needed
322323
//TODO cache the underlying regexp
323324
return getExactOverriddenForeignKey( propertyName.replace( ".{element}.", "." ) );
324325
}
325-
return result;
326+
else {
327+
return result;
328+
}
326329
}
327330

328331
private ForeignKey getExactOverriddenForeignKey(String propertyName) {
@@ -585,7 +588,7 @@ private static Map<String, ColumnTransformer> buildColumnTransformerOverride(Ann
585588
private static Map<String, JoinColumn[]> buildJoinColumnOverride(AnnotationTarget element, String path, MetadataBuildingContext context) {
586589
final Map<String, JoinColumn[]> columnOverride = new HashMap<>();
587590
if ( element != null ) {
588-
for ( var override : buildAssociationOverrides( element, path, context ) ) {
591+
for ( var override : buildAssociationOverrides( element, context ) ) {
589592
columnOverride.put( qualify( path, override.name() ), override.joinColumns() );
590593
}
591594
}
@@ -595,21 +598,21 @@ private static Map<String, JoinColumn[]> buildJoinColumnOverride(AnnotationTarge
595598
private static Map<String, ForeignKey> buildForeignKeyOverride(AnnotationTarget element, String path, MetadataBuildingContext context) {
596599
final Map<String, ForeignKey> foreignKeyOverride = new HashMap<>();
597600
if ( element != null ) {
598-
for ( var override : buildAssociationOverrides( element, path, context ) ) {
601+
for ( var override : buildAssociationOverrides( element, context ) ) {
599602
foreignKeyOverride.put( qualify( path, override.name() ), override.foreignKey() );
600603
}
601604
}
602605
return foreignKeyOverride;
603606
}
604607

605-
private static AssociationOverride[] buildAssociationOverrides(AnnotationTarget element, String path, MetadataBuildingContext context) {
608+
private static AssociationOverride[] buildAssociationOverrides(AnnotationTarget element, MetadataBuildingContext context) {
606609
return element.getRepeatedAnnotationUsages( AssociationOverride.class, context.getBootstrapContext().getModelsContext() );
607610
}
608611

609612
private static Map<String, JoinTable> buildJoinTableOverride(AnnotationTarget element, String path, MetadataBuildingContext context) {
610613
final Map<String, JoinTable> result = new HashMap<>();
611614
if ( element != null ) {
612-
for ( var override : buildAssociationOverrides( element, path, context ) ) {
615+
for ( var override : buildAssociationOverrides( element, context ) ) {
613616
if ( isEmpty( override.joinColumns() ) ) {
614617
result.put( qualify( path, override.name() ), override.joinTable() );
615618
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumn.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static AnnotatedJoinColumn buildJoinColumn(
7373
PropertyHolder propertyHolder,
7474
PropertyData inferredData) {
7575
final String path = qualify( propertyHolder.getPath(), inferredData.getPropertyName() );
76-
final JoinColumn[] overrides = propertyHolder.getOverriddenJoinColumn( path );
76+
final var overrides = propertyHolder.getOverriddenJoinColumn( path );
7777
if ( overrides != null ) {
7878
//TODO: relax this restriction
7979
throw new AnnotationException( "Property '" + path
@@ -334,7 +334,7 @@ public void linkValueUsingDefaultColumnNaming(
334334
setLogicalColumnName( columnName );
335335
setImplicit( true );
336336
setReferencedColumn( logicalReferencedColumn );
337-
final Column mappingColumn = getMappingColumn();
337+
final var mappingColumn = getMappingColumn();
338338
initMappingColumn(
339339
columnName,
340340
null,
@@ -363,7 +363,7 @@ private String defaultColumnName(int columnIndex, PersistentClass referencedEnti
363363
// the name of the @MapsId annotation, but
364364
// it's better than just having two different
365365
// column names which disagree
366-
final Column column = parent.resolveMapsId().getValue().getColumns().get( columnIndex );
366+
final var column = parent.resolveMapsId().getValue().getColumns().get( columnIndex );
367367
// return column.getQuotedName();
368368
if ( column.isExplicit() ) {
369369
throw new AnnotationException( "Association '" + parent.getPropertyName()
@@ -429,7 +429,7 @@ protected void addColumnBinding(SimpleValue value) {
429429
* @param column the referenced column.
430430
*/
431431
public void overrideFromReferencedColumnIfNecessary(Column column) {
432-
final Column mappingColumn = getMappingColumn();
432+
final var mappingColumn = getMappingColumn();
433433
if ( mappingColumn != null ) {
434434
// columnDefinition can also be specified using @JoinColumn, hence we have to check
435435
// whether it is set or not

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedJoinColumns.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildExplicitJoinTableJoinColumn;
4141
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildImplicitJoinTableJoinColumn;
4242
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildJoinColumn;
43+
import static org.hibernate.boot.model.internal.AnnotatedJoinColumn.buildJoinFormula;
4344
import static org.hibernate.boot.model.internal.BinderHelper.findReferencedColumnOwner;
4445
import static org.hibernate.boot.model.internal.BinderHelper.getRelativePath;
4546
import static org.hibernate.boot.model.internal.ForeignKeyType.EXPLICIT_PRIMARY_KEY_REFERENCE;
@@ -99,7 +100,7 @@ public static AnnotatedJoinColumns buildJoinColumnsOrFormulas(
99100
final var column = columnOrFormula.column();
100101
final String annotationString = formula.value();
101102
if ( isNotBlank( annotationString ) ) {
102-
AnnotatedJoinColumn.buildJoinFormula( formula, parent );
103+
buildJoinFormula( formula, parent );
103104
}
104105
else {
105106
buildJoinColumn( column, mappedBy, parent, propertyHolder, inferredData );
@@ -134,7 +135,7 @@ static AnnotatedJoinColumns buildJoinColumnsWithFormula(
134135
joinColumns.setJoins( secondaryTables );
135136
joinColumns.setPropertyHolder( propertyHolder );
136137
joinColumns.setPropertyName( getRelativePath( propertyHolder, inferredData.getPropertyName() ) );
137-
AnnotatedJoinColumn.buildJoinFormula( joinFormula, joinColumns );
138+
buildJoinFormula( joinFormula, joinColumns );
138139
handlePropertyRef( inferredData.getAttributeMember(), joinColumns );
139140
return joinColumns;
140141
}
@@ -168,15 +169,15 @@ public static AnnotatedJoinColumns buildJoinColumnsWithDefaultColumnSuffix(
168169
assert mappedBy == null || !mappedBy.isBlank();
169170
final String propertyName = inferredData.getPropertyName();
170171
final String path = qualify( propertyHolder.getPath(), propertyName );
171-
final JoinColumn[] overrides = propertyHolder.getOverriddenJoinColumn( path );
172-
final JoinColumn[] actualColumns = overrides == null ? joinColumns : overrides;
172+
final var overriddenJoinColumns = propertyHolder.getOverriddenJoinColumn( path );
173+
final var actualJoinColumns = overriddenJoinColumns == null ? joinColumns : overriddenJoinColumns;
173174
final var parent = new AnnotatedJoinColumns();
174175
parent.setBuildingContext( context );
175176
parent.setJoins( joins );
176177
parent.setPropertyHolder( propertyHolder );
177178
parent.setPropertyName( getRelativePath( propertyHolder, propertyName ) );
178179
parent.setMappedBy( mappedBy );
179-
if ( isEmpty( actualColumns ) ) {
180+
if ( isEmpty( actualJoinColumns ) ) {
180181
buildJoinColumn(
181182
null,
182183
mappedBy,
@@ -188,7 +189,7 @@ public static AnnotatedJoinColumns buildJoinColumnsWithDefaultColumnSuffix(
188189
}
189190
else {
190191
parent.setMappedBy( mappedBy );
191-
for ( var actualColumn : actualColumns ) {
192+
for ( var actualColumn : actualJoinColumns ) {
192193
buildJoinColumn(
193194
actualColumn,
194195
mappedBy,

hibernate-core/src/main/java/org/hibernate/boot/model/internal/ComponentPropertyHolder.java

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.hibernate.annotations.EmbeddedTable;
1313
import org.hibernate.boot.model.naming.Identifier;
1414
import org.hibernate.boot.models.AnnotationPlacementException;
15-
import org.hibernate.boot.spi.InFlightMetadataCollector;
1615
import org.hibernate.boot.spi.MetadataBuildingContext;
1716
import org.hibernate.boot.spi.PropertyData;
1817
import org.hibernate.mapping.AggregateColumn;
@@ -118,47 +117,48 @@ public static void applyExplicitTableName(
118117
PropertyHolder container,
119118
MetadataBuildingContext buildingContext) {
120119
Table tableToUse = container.getTable();
121-
boolean wasExplicit = false;
122-
if ( container instanceof ComponentPropertyHolder componentPropertyHolder ) {
123-
wasExplicit = componentPropertyHolder.getComponent().wasTableExplicitlyDefined();
124-
}
120+
boolean wasExplicit =
121+
container instanceof ComponentPropertyHolder componentPropertyHolder
122+
&& componentPropertyHolder.getComponent().wasTableExplicitlyDefined();
125123

126-
if ( propertyData.getAttributeMember() != null ) {
127-
final EmbeddedTable embeddedTableAnn = propertyData.getAttributeMember()
128-
.getDirectAnnotationUsage( EmbeddedTable.class );
124+
final var attributeMember = propertyData.getAttributeMember();
125+
if ( attributeMember != null ) {
126+
final var embeddedTableAnn = attributeMember.getDirectAnnotationUsage( EmbeddedTable.class );
129127
// we only allow this when done for an embedded on an entity or mapped-superclass
130128
if ( container instanceof ClassPropertyHolder ) {
131129
if ( embeddedTableAnn != null ) {
132-
final Identifier tableNameIdentifier = buildingContext.getObjectNameNormalizer().normalizeIdentifierQuoting( embeddedTableAnn.value() );
133-
final InFlightMetadataCollector.EntityTableXref entityTableXref = buildingContext
134-
.getMetadataCollector()
135-
.getEntityTableXref( container.getEntityName() );
136-
tableToUse = entityTableXref.resolveTable( tableNameIdentifier );
130+
tableToUse = resolveEmbeddedTable( container, buildingContext, embeddedTableAnn );
137131
wasExplicit = true;
138132
}
139133
}
140-
else {
141-
if ( embeddedTableAnn != null ) {
142-
// not allowed
143-
throw new AnnotationPlacementException( "@EmbeddedTable only supported for use on entity or mapped-superclass" );
144-
}
134+
else if ( embeddedTableAnn != null ) {
135+
// not allowed
136+
throw new AnnotationPlacementException(
137+
"@EmbeddedTable only supported for use on entity or mapped-superclass" );
145138
}
146139
}
147-
if ( propertyData.getAttributeMember() != null && container instanceof ClassPropertyHolder ) {
148-
final EmbeddedTable embeddedTableAnn = propertyData.getAttributeMember().getDirectAnnotationUsage( EmbeddedTable.class );
140+
if ( attributeMember != null && container instanceof ClassPropertyHolder ) {
141+
final var embeddedTableAnn = attributeMember.getDirectAnnotationUsage( EmbeddedTable.class );
149142
if ( embeddedTableAnn != null ) {
150-
final Identifier tableNameIdentifier = buildingContext.getObjectNameNormalizer().normalizeIdentifierQuoting( embeddedTableAnn.value() );
151-
final InFlightMetadataCollector.EntityTableXref entityTableXref = buildingContext
152-
.getMetadataCollector()
153-
.getEntityTableXref( container.getEntityName() );
154-
tableToUse = entityTableXref.resolveTable( tableNameIdentifier );
143+
tableToUse = resolveEmbeddedTable( container, buildingContext, embeddedTableAnn );
155144
wasExplicit = true;
156145
}
157146
}
158147

159148
component.setTable( tableToUse, wasExplicit );
160149
}
161150

151+
private static Table resolveEmbeddedTable(
152+
PropertyHolder container, MetadataBuildingContext buildingContext, EmbeddedTable embeddedTableAnn) {
153+
final Identifier tableNameIdentifier =
154+
buildingContext.getObjectNameNormalizer()
155+
.normalizeIdentifierQuoting( embeddedTableAnn.value() );
156+
final var entityTableXref =
157+
buildingContext.getMetadataCollector()
158+
.getEntityTableXref( container.getEntityName() );
159+
return entityTableXref.resolveTable( tableNameIdentifier );
160+
}
161+
162162
/**
163163
* Access to the underlying component
164164
*/
@@ -235,21 +235,19 @@ protected String normalizeCompositePathForLogging(String attributeName) {
235235
public void startingProperty(MemberDetails propertyMemberDetails) {
236236
if ( propertyMemberDetails != null ) {
237237
// again: the property coming in here *should* be the property on the embeddable (Address#city in the example),
238-
// so we just ignore it if there is already an existing conversion info for that path since they would have
238+
// so we just ignore it if there is already an existing ConversionInfo for that path since they would have
239239
// precedence
240240

241241
// technically we should only do this for properties of "basic type"
242242

243243
final String attributeName = propertyMemberDetails.resolveAttributeName();
244244
final String path = embeddedAttributeName + '.' + attributeName;
245-
if ( attributeConversionInfoMap.containsKey( path ) ) {
246-
return;
245+
if ( !attributeConversionInfoMap.containsKey( path ) ) {
246+
propertyMemberDetails.forEachAnnotationUsage( Convert.class, getSourceModelContext(), (usage) -> {
247+
final var info = new AttributeConversionInfo( usage, propertyMemberDetails );
248+
attributeConversionInfoMap.put( attributeName, info );
249+
} );
247250
}
248-
249-
propertyMemberDetails.forEachAnnotationUsage( Convert.class, getSourceModelContext(), (usage) -> {
250-
final AttributeConversionInfo info = new AttributeConversionInfo( usage, propertyMemberDetails );
251-
attributeConversionInfoMap.put( attributeName, info );
252-
} );
253251
}
254252
}
255253

@@ -403,7 +401,7 @@ public void setParentProperty(String parentProperty) {
403401
@Override
404402
public Column[] getOverriddenColumn(String propertyName) {
405403
//FIXME this is yukky
406-
Column[] result = super.getOverriddenColumn( propertyName );
404+
var result = super.getOverriddenColumn( propertyName );
407405
if ( result == null ) {
408406
final String userPropertyName = extractUserPropertyName( "id", propertyName );
409407
if ( userPropertyName != null ) {
@@ -434,6 +432,7 @@ private String extractUserPropertyName(String redundantString, String propertyNa
434432

435433
@Override
436434
public String toString() {
437-
return getClass().getSimpleName() + "(" + parent.normalizeCompositePathForLogging( embeddedAttributeName ) + ")";
435+
return getClass().getSimpleName()
436+
+ "(" + parent.normalizeCompositePathForLogging( embeddedAttributeName ) + ")";
438437
}
439438
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyHolder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ public interface PropertyHolder {
6161
String getPath();
6262

6363
/**
64-
* return null if the column is not overridden, or an array of column if true
64+
* return null if the column is not overridden,
65+
* or an array of columns if it is
6566
*/
6667
Column[] getOverriddenColumn(String propertyName);
6768

6869
/**
69-
* return null if the column is not overridden, or an array of column if true
70+
* return null if the column is not overridden,
71+
* or an array of columns if it is
7072
*/
7173
JoinColumn[] getOverriddenJoinColumn(String propertyName);
7274

0 commit comments

Comments
 (0)