Skip to content

Commit b253460

Browse files
committed
some cleanups around property accessors
- get rid of use of raw strings to identify strategies - lots of 'var' - annotate some stuff @serial - modernize an enum - etc.
1 parent 0fa5e6f commit b253460

File tree

43 files changed

+400
-406
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+400
-406
lines changed

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

Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.hibernate.annotations.OnDeleteAction;
2323
import org.hibernate.annotations.SqlFragmentAlias;
2424
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
25-
import org.hibernate.boot.spi.InFlightMetadataCollector;
2625
import org.hibernate.boot.spi.MetadataBuildingContext;
2726
import org.hibernate.boot.spi.PropertyData;
2827
import org.hibernate.internal.util.collections.ArrayHelper;
@@ -74,7 +73,6 @@
7473
import static org.hibernate.models.spi.TypeDetailsHelper.resolveRawClass;
7574
import static org.hibernate.property.access.spi.BuiltInPropertyAccessStrategies.EMBEDDED;
7675
import static org.hibernate.property.access.spi.BuiltInPropertyAccessStrategies.NOOP;
77-
import static org.hibernate.property.access.spi.BuiltInPropertyAccessStrategies.interpret;
7876

7977
/**
8078
* @author Emmanuel Bernard
@@ -268,29 +266,23 @@ private static void registerSyntheticProperty(
268266
String propertyName,
269267
String syntheticPropertyName,
270268
MetadataBuildingContext context) {
269+
final var collector = context.getMetadataCollector();
271270
if ( value instanceof ToOne toOne ) {
272271
toOne.setReferencedPropertyName( syntheticPropertyName );
273272
toOne.setReferenceToPrimaryKey( false );
274-
context.getMetadataCollector().addUniquePropertyReference(
275-
ownerEntity.getEntityName(),
276-
syntheticPropertyName
277-
);
273+
collector.addUniquePropertyReference( ownerEntity.getEntityName(), syntheticPropertyName );
278274
}
279275
else if ( value instanceof Collection collection ) {
280276
collection.setReferencedPropertyName( syntheticPropertyName );
281277
//not unique because we could create a mtm wo association table
282-
context.getMetadataCollector().addPropertyReference(
283-
ownerEntity.getEntityName(),
284-
syntheticPropertyName
285-
);
278+
collector.addPropertyReference( ownerEntity.getEntityName(), syntheticPropertyName );
286279
}
287280
else {
288281
throw new AssertionFailure( "Property ref on an unexpected Value type: " + value.getClass().getName() );
289282
}
290283
final String associatedEntityName = associatedClass.getEntityName();
291284
final String generatedName = inverse ? "inverse__" + associatedEntityName : associatedEntityName;
292-
context.getMetadataCollector()
293-
.addPropertyReferencedAssociation( generatedName, propertyName, syntheticPropertyName );
285+
collector.addPropertyReferencedAssociation( generatedName, propertyName, syntheticPropertyName );
294286
}
295287

296288
private static String syntheticPropertyName(
@@ -311,12 +303,9 @@ private static String associationMessage(PersistentClass associatedEntity, Annot
311303
}
312304
else {
313305
final PropertyHolder propertyHolder = joinColumns.getPropertyHolder();
314-
if ( propertyHolder != null ) {
315-
return "'" + propertyHolder.getEntityName() + "." + joinColumns.getPropertyName() + "'";
316-
}
317-
else {
318-
return "";
319-
}
306+
return propertyHolder != null
307+
? "'" + propertyHolder.getEntityName() + "." + joinColumns.getPropertyName() + "'"
308+
: "";
320309
}
321310
}
322311

@@ -354,7 +343,7 @@ else if ( persistentClassOrJoin instanceof Join join ) {
354343
result.setUpdateable( false );
355344
result.setInsertable( false );
356345
result.setValue( embeddedComponent );
357-
result.setPropertyAccessorName( "embedded" );
346+
result.setPropertyAccessorName( EMBEDDED.getExternalName() );
358347
if ( persistentClassOrJoin instanceof Join ) {
359348
// the referenced column is in the joined table, add the synthetic property there
360349
persistentClassOrJoin.addProperty( result );
@@ -433,10 +422,10 @@ private static List<Property> findPropertiesByColumns(
433422
// specified by the @JoinColumn annotations.
434423
final List<Column> orderedColumns = new ArrayList<>( columns.getJoinColumns().size() );
435424
final Map<Column, Set<Property>> columnsToProperty = new HashMap<>();
436-
final InFlightMetadataCollector collector = context.getMetadataCollector();
437-
for ( AnnotatedJoinColumn joinColumn : columns.getJoinColumns() ) {
425+
final var collector = context.getMetadataCollector();
426+
for ( var joinColumn : columns.getJoinColumns() ) {
438427
if ( joinColumn.isReferenceImplicit() ) {
439-
throw new AnnotationException("Association " + associationMessage( associatedEntity, columns )
428+
throw new AnnotationException( "Association " + associationMessage( associatedEntity, columns )
440429
+ " has a '@JoinColumn' which does not specify the 'referencedColumnName'"
441430
+ " (when an association has multiple '@JoinColumn's, they must each specify their 'referencedColumnName')");
442431
}
@@ -554,28 +543,28 @@ else if ( orderedProperties.contains( property ) ) {
554543
}
555544

556545
private static void matchColumnsByProperty(Property property, Map<Column, Set<Property>> columnsToProperty) {
557-
if ( property != null
558-
&& NOOP != interpret( property.getPropertyAccessorName() )
559-
&& EMBEDDED != interpret( property.getPropertyAccessorName() ) ) {
560-
//TODO: we can't return subproperties because the caller
561-
// needs top level properties, but this results in
562-
// a limitation where I need to be referencing all
563-
// columns of an embeddable instead of just some
564-
// if ( property.isComposite() ) {
565-
// for ( Property sp : ( (Component) property.getValue() ).getProperties() ) {
566-
// matchColumnsByProperty( sp, columnsToProperty );
546+
if ( property != null ) {
547+
final String propertyAccessorName = property.getPropertyAccessorName();
548+
if ( !NOOP.getExternalName().equals( propertyAccessorName )
549+
&& !EMBEDDED.getExternalName().equals( propertyAccessorName ) ) {
550+
//TODO: we can't return subproperties because the caller
551+
// needs top level properties, but this results in
552+
// a limitation where I need to be referencing all
553+
// columns of an embeddable instead of just some
554+
// if ( property.isComposite() ) {
555+
// for ( Property sp : ( (Component) property.getValue() ).getProperties() ) {
556+
// matchColumnsByProperty( sp, columnsToProperty );
557+
// }
567558
// }
568-
// }
569-
// else {
570-
for ( Selectable selectable : property.getSelectables() ) {
571-
//can be a Formula, so we don't cast
572-
//noinspection SuspiciousMethodCalls
573-
if ( columnsToProperty.containsKey( selectable ) ) {
574-
//noinspection SuspiciousMethodCalls
575-
columnsToProperty.get( selectable ).add( property );
559+
// else {
560+
for ( Selectable selectable : property.getSelectables() ) {
561+
if ( selectable instanceof Column column
562+
&& columnsToProperty.containsKey( column ) ) {
563+
columnsToProperty.get( column ).add( property );
564+
}
576565
}
566+
// }
577567
}
578-
// }
579568
}
580569
}
581570

@@ -597,9 +586,9 @@ public static Property findPropertyByName(PersistentClass associatedClass, Strin
597586
property = idProperty;
598587
propertyName = propertyName.substring( idName.length() + 1 );
599588
}
600-
final StringTokenizer tokens = new StringTokenizer( propertyName, ".", false );
601-
while ( tokens.hasMoreElements() ) {
602-
String element = (String) tokens.nextElement();
589+
final var tokens = new StringTokenizer( propertyName, ".", false );
590+
while ( tokens.hasMoreTokens() ) {
591+
final String element = tokens.nextToken();
603592
if ( property == null ) {
604593
property = associatedClass.getProperty( element );
605594
}
@@ -614,13 +603,13 @@ public static Property findPropertyByName(PersistentClass associatedClass, Strin
614603
}
615604
catch ( MappingException e ) {
616605
try {
617-
//if we do not find it try to check the identifier mapper
606+
// if we do not find it, try to check the identifier mapper
618607
if ( associatedClass.getIdentifierMapper() == null ) {
619608
return null;
620609
}
621-
final StringTokenizer tokens = new StringTokenizer( propertyName, ".", false );
622-
while ( tokens.hasMoreElements() ) {
623-
final String element = (String) tokens.nextElement();
610+
final var tokens = new StringTokenizer( propertyName, ".", false );
611+
while ( tokens.hasMoreTokens() ) {
612+
final String element = tokens.nextToken();
624613
if ( property == null ) {
625614
property = associatedClass.getIdentifierMapper().getProperty( element );
626615
}
@@ -650,9 +639,9 @@ public static Property findPropertyByName(Component component, String propertyNa
650639
return null;
651640
}
652641
else {
653-
final StringTokenizer tokens = new StringTokenizer( propertyName, ".", false );
654-
while ( tokens.hasMoreElements() ) {
655-
final String element = (String) tokens.nextElement();
642+
final var tokens = new StringTokenizer( propertyName, ".", false );
643+
while ( tokens.hasMoreTokens() ) {
644+
final String element = tokens.nextToken();
656645
if ( property == null ) {
657646
property = component.getProperty( element );
658647
}
@@ -667,13 +656,13 @@ public static Property findPropertyByName(Component component, String propertyNa
667656
}
668657
catch (MappingException e) {
669658
try {
670-
//if we do not find it try to check the identifier mapper
659+
// if we do not find it, try to check the identifier mapper
671660
if ( component.getOwner().getIdentifierMapper() == null ) {
672661
return null;
673662
}
674-
final StringTokenizer tokens = new StringTokenizer( propertyName, ".", false );
675-
while ( tokens.hasMoreElements() ) {
676-
final String element = (String) tokens.nextElement();
663+
final var tokens = new StringTokenizer( propertyName, ".", false );
664+
while ( tokens.hasMoreTokens() ) {
665+
final String element = tokens.nextToken();
677666
if ( property == null ) {
678667
property = component.getOwner().getIdentifierMapper().getProperty( element );
679668
}
@@ -722,7 +711,7 @@ public static AttributeContainer findColumnOwner(
722711
PersistentClass persistentClass,
723712
String columnName,
724713
MetadataBuildingContext context) {
725-
final InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
714+
final var metadataCollector = context.getMetadataCollector();
726715
PersistentClass current = persistentClass;
727716
while ( current != null ) {
728717
try {
@@ -916,8 +905,7 @@ public static EnumSet<CascadeType> aggregateCascadeTypes(
916905
Cascade cascadeAnnotation,
917906
boolean orphanRemoval,
918907
MetadataBuildingContext context) {
919-
final EnumSet<CascadeType> cascades =
920-
convertToHibernateCascadeType( cascadeTypes );
908+
final var cascades = convertToHibernateCascadeType( cascadeTypes );
921909
final CascadeType[] hibernateCascades =
922910
cascadeAnnotation == null
923911
? null
@@ -937,7 +925,7 @@ public static EnumSet<CascadeType> aggregateCascadeTypes(
937925
}
938926

939927
private static EnumSet<CascadeType> convertToHibernateCascadeType(jakarta.persistence.CascadeType[] cascades) {
940-
final EnumSet<CascadeType> cascadeTypes = EnumSet.noneOf( CascadeType.class );
928+
final var cascadeTypes = EnumSet.noneOf( CascadeType.class );
941929
if ( cascades != null ) {
942930
for ( jakarta.persistence.CascadeType cascade: cascades ) {
943931
cascadeTypes.add( convertCascadeType( cascade ) );
@@ -1076,14 +1064,12 @@ public static <A extends Annotation> A extractFromPackage(
10761064
// where context.getMetadataCollector() can cache some of this - either the annotations themselves
10771065
// or even just the XPackage resolutions
10781066

1079-
final String declaringClassName = classDetails.getName();
1080-
final String packageName = qualifier( declaringClassName );
1067+
final String packageName = qualifier( classDetails.getName() );
10811068
if ( isEmpty( packageName ) ) {
10821069
return null;
10831070
}
10841071
else {
1085-
final ModelsContext modelsContext =
1086-
context.getBootstrapContext().getModelsContext();
1072+
final var modelsContext = context.getBootstrapContext().getModelsContext();
10871073
try {
10881074
return modelsContext.getClassDetailsRegistry()
10891075
.resolveClassDetails( packageName + ".package-info" )

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildFormulaFromAnnotation;
100100
import static org.hibernate.boot.model.internal.AnnotatedJoinColumns.buildJoinColumnsWithDefaultColumnSuffix;
101101
import static org.hibernate.boot.model.internal.AnnotatedJoinColumns.buildJoinTableJoinColumns;
102+
import static org.hibernate.boot.model.internal.BasicValueBinder.Kind.COLLECTION_ELEMENT;
102103
import static org.hibernate.boot.model.internal.BinderHelper.aggregateCascadeTypes;
103104
import static org.hibernate.boot.model.internal.BinderHelper.buildAnyValue;
104105
import static org.hibernate.boot.model.internal.BinderHelper.checkMappedByType;
@@ -123,6 +124,7 @@
123124
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
124125
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
125126
import static org.hibernate.mapping.MappingHelper.createUserTypeBean;
127+
import static org.hibernate.property.access.spi.BuiltInPropertyAccessStrategies.BASIC;
126128

127129
/**
128130
* Base class for stateful binders responsible for producing mapping model objects of type {@link Collection}.
@@ -2115,9 +2117,7 @@ private void handleElementCollection(TypeDetails elementType, String hqlOrderBy)
21152117
// 'property' is the collection XProperty
21162118

21172119
final boolean isPrimitive = isPrimitive( elementType.getName() );
2118-
final ClassDetails elementClass = isPrimitive
2119-
? null
2120-
: elementType.determineRawClass();
2120+
final ClassDetails elementClass = isPrimitive ? null : elementType.determineRawClass();
21212121
final AnnotatedClassType classType = annotatedElementType( isEmbedded, isPrimitive, property, elementClass );
21222122
if ( !isPrimitive ) {
21232123
propertyHolder.startingProperty( property );
@@ -2126,8 +2126,7 @@ private void handleElementCollection(TypeDetails elementType, String hqlOrderBy)
21262126
final CollectionPropertyHolder holder =
21272127
buildPropertyHolder( collection, getRole(), elementClass, property, propertyHolder, buildingContext );
21282128

2129-
final Class<? extends CompositeUserType<?>> compositeUserType =
2130-
resolveCompositeUserType( property, elementClass, buildingContext );
2129+
final var compositeUserType = resolveCompositeUserType( property, elementClass, buildingContext );
21312130
final boolean isComposite = classType == EMBEDDABLE || compositeUserType != null;
21322131
holder.prepare( property, isComposite );
21332132

@@ -2144,8 +2143,7 @@ private void handleCollectionElement(
21442143
String hqlOrderBy,
21452144
ClassDetails elementClass,
21462145
CollectionPropertyHolder holder) {
2147-
final BasicValueBinder elementBinder =
2148-
new BasicValueBinder( BasicValueBinder.Kind.COLLECTION_ELEMENT, buildingContext );
2146+
final var elementBinder = new BasicValueBinder( COLLECTION_ELEMENT, buildingContext );
21492147
elementBinder.setReturnedClassName( elementType.getName() );
21502148
final AnnotatedColumns actualColumns = createElementColumnsIfNecessary(
21512149
collection,
@@ -2177,10 +2175,10 @@ private void handleCompositeCollectionElement(
21772175
Class<? extends CompositeUserType<?>> compositeUserType) {
21782176
//TODO be smart with isNullable
21792177
final AccessType accessType = accessType( property, collection.getOwner() );
2180-
// We create a new entity binder here because it is needed for processing the embeddable
2178+
// We create a new entity binder here because it's needed for processing the embeddable
21812179
// Since this is an element collection, there is no real entity binder though,
21822180
// so we just create an "empty shell" for the purpose of avoiding null checks in the fillEmbeddable() method etc.
2183-
final EntityBinder entityBinder = new EntityBinder( buildingContext );
2181+
final var entityBinder = new EntityBinder( buildingContext );
21842182
// Copy over the access type that we resolve for the element collection,
21852183
// so that nested components use the same access type. This fixes HHH-15966
21862184
entityBinder.setPropertyAccessType( accessType );
@@ -2216,20 +2214,27 @@ static AccessType accessType(MemberDetails property, PersistentClass owner) {
22162214
? AccessType.PROPERTY
22172215
: AccessType.FIELD;
22182216
}
2219-
else if ( owner.getIdentifierProperty() != null ) {
2220-
// use the access for the owning entity's id attribute, if one
2221-
return owner.getIdentifierProperty().getPropertyAccessorName().equals( "property" )
2222-
? AccessType.PROPERTY
2223-
: AccessType.FIELD;
2224-
}
2225-
else if ( owner.getIdentifierMapper() != null && owner.getIdentifierMapper().getPropertySpan() > 0 ) {
2226-
// use the access for the owning entity's "id mapper", if one
2227-
return owner.getIdentifierMapper().getProperties().get(0).getPropertyAccessorName().equals( "property" )
2228-
? AccessType.PROPERTY
2229-
: AccessType.FIELD;
2230-
}
22312217
else {
2232-
throw new AssertionFailure( "Unable to guess collection property accessor name" );
2218+
final Property identifierProperty = owner.getIdentifierProperty();
2219+
if ( identifierProperty != null ) {
2220+
// use the access for the owning entity's id attribute, if one
2221+
return identifierProperty.getPropertyAccessorName().equals( BASIC.getExternalName() )
2222+
? AccessType.PROPERTY
2223+
: AccessType.FIELD;
2224+
}
2225+
else {
2226+
final Component identifierMapper = owner.getIdentifierMapper();
2227+
if ( identifierMapper != null && identifierMapper.getPropertySpan() > 0 ) {
2228+
// use the access for the owning entity's "id mapper"
2229+
final Property first = identifierMapper.getProperties().get( 0 );
2230+
return first.getPropertyAccessorName().equals( BASIC.getExternalName() )
2231+
? AccessType.PROPERTY
2232+
: AccessType.FIELD;
2233+
}
2234+
else {
2235+
throw new AssertionFailure( "Unable to guess collection property accessor name" );
2236+
}
2237+
}
22332238
}
22342239
}
22352240

0 commit comments

Comments
 (0)