Skip to content

Commit 6eba60e

Browse files
committed
even more refactoring to ModelBinder
1 parent 79d98c2 commit 6eba60e

File tree

1 file changed

+59
-79
lines changed
  • hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm

1 file changed

+59
-79
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java

Lines changed: 59 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,19 +1059,16 @@ private void handleNaturalIdBinding(
10591059
NaturalIdMutability naturalIdMutability) {
10601060
if ( naturalIdMutability != NaturalIdMutability.NOT_NATURAL_ID ) {
10611061
attributeBinding.setNaturalIdentifier( true );
1062-
10631062
if ( naturalIdMutability == NaturalIdMutability.IMMUTABLE ) {
10641063
attributeBinding.setUpdatable( false );
10651064
}
1066-
10671065
final var metadataCollector = mappingDocument.getMetadataCollector();
10681066
final String entityName = entityBinding.getEntityName();
10691067
var ukBinder = metadataCollector.locateNaturalIdUniqueKeyBinder( entityName );
10701068
if ( ukBinder == null ) {
10711069
ukBinder = new NaturalIdUniqueKeyBinderImpl( mappingDocument, entityBinding );
10721070
metadataCollector.registerNaturalIdUniqueKeyBinder( entityName, ukBinder );
10731071
}
1074-
10751072
ukBinder.addAttributeBinding( attributeBinding );
10761073
}
10771074
}
@@ -1081,12 +1078,10 @@ private Property createPluralAttribute(
10811078
PluralAttributeSource attributeSource,
10821079
PersistentClass entityDescriptor) {
10831080
final Collection collectionBinding;
1084-
10851081
if ( attributeSource instanceof PluralAttributeSourceListImpl pluralAttributeSourceList ) {
10861082
final var list = new org.hibernate.mapping.List(sourceDocument, entityDescriptor);
10871083
collectionBinding = list;
10881084
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );
1089-
10901085
registerSecondPass(
10911086
new PluralAttributeListSecondPass( sourceDocument, pluralAttributeSourceList, list ),
10921087
sourceDocument
@@ -1095,7 +1090,6 @@ private Property createPluralAttribute(
10951090
else if ( attributeSource instanceof PluralAttributeSourceSetImpl ) {
10961091
collectionBinding = new Set( sourceDocument, entityDescriptor );
10971092
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );
1098-
10991093
registerSecondPass(
11001094
new PluralAttributeSetSecondPass( sourceDocument, attributeSource, collectionBinding ),
11011095
sourceDocument
@@ -1105,7 +1099,6 @@ else if ( attributeSource instanceof PluralAttributeSourceMapImpl pluralAttribut
11051099
final var map = new org.hibernate.mapping.Map( sourceDocument, entityDescriptor );
11061100
collectionBinding = map;
11071101
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );
1108-
11091102
registerSecondPass(
11101103
new PluralAttributeMapSecondPass( sourceDocument, pluralAttributeSourceMap, map ),
11111104
sourceDocument
@@ -1114,7 +1107,6 @@ else if ( attributeSource instanceof PluralAttributeSourceMapImpl pluralAttribut
11141107
else if ( attributeSource instanceof PluralAttributeSourceBagImpl ) {
11151108
collectionBinding = new Bag( sourceDocument, entityDescriptor );
11161109
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );
1117-
11181110
registerSecondPass(
11191111
new PluralAttributeBagSecondPass( sourceDocument, attributeSource, collectionBinding ),
11201112
sourceDocument
@@ -1123,7 +1115,6 @@ else if ( attributeSource instanceof PluralAttributeSourceBagImpl ) {
11231115
else if ( attributeSource instanceof PluralAttributeSourceIdBagImpl ) {
11241116
collectionBinding = new IdentifierBag( sourceDocument, entityDescriptor );
11251117
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );
1126-
11271118
registerSecondPass(
11281119
new PluralAttributeIdBagSecondPass( sourceDocument, attributeSource, collectionBinding ),
11291120
sourceDocument
@@ -1133,9 +1124,7 @@ else if ( attributeSource instanceof PluralAttributeSourceArrayImpl arraySource
11331124
final var array = new Array(sourceDocument, entityDescriptor);
11341125
collectionBinding = array;
11351126
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );
1136-
11371127
array.setElementClassName( sourceDocument.qualifyClassName( arraySource.getElementClass() ) );
1138-
11391128
registerSecondPass(
11401129
new PluralAttributeArraySecondPass( sourceDocument, arraySource, array ),
11411130
sourceDocument
@@ -1145,7 +1134,6 @@ else if ( attributeSource instanceof PluralAttributeSourcePrimitiveArrayImpl plu
11451134
final var primitiveArray = new PrimitiveArray( sourceDocument, entityDescriptor );
11461135
collectionBinding = primitiveArray;
11471136
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );
1148-
11491137
registerSecondPass(
11501138
new PluralAttributePrimitiveArraySecondPass(
11511139
sourceDocument,
@@ -1161,16 +1149,17 @@ else if ( attributeSource instanceof PluralAttributeSourcePrimitiveArrayImpl plu
11611149
);
11621150
}
11631151

1164-
sourceDocument.getMetadataCollector().addCollectionBinding( collectionBinding );
1152+
sourceDocument.getMetadataCollector()
1153+
.addCollectionBinding( collectionBinding );
11651154

11661155
final var attribute = new Property();
11671156
attribute.setValue( collectionBinding );
11681157
bindProperty( sourceDocument, attributeSource, attribute );
1169-
11701158
return attribute;
11711159
}
11721160

1173-
private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) {
1161+
private void bindCollectionMetadata(
1162+
MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) {
11741163
binding.setRole( source.getAttributeRole().getFullPath() );
11751164
binding.setInverse( source.isInverse() );
11761165
binding.setMutable( source.isMutable() );
@@ -1186,40 +1175,11 @@ private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttri
11861175

11871176
applyCaching( mappingDocument, source.getCaching(), binding );
11881177

1189-
// bind the collection type info
1190-
String typeName = source.getTypeInformation().getName();
1191-
final Map<String,String> typeParameters = new HashMap<>();
1192-
if ( typeName != null ) {
1193-
// see if there is a corresponding type-def
1194-
final var typeDefinition =
1195-
mappingDocument.getMetadataCollector().getTypeDefinition( typeName );
1196-
if ( typeDefinition != null ) {
1197-
typeName = typeDefinition.getTypeImplementorClass().getName();
1198-
if ( typeDefinition.getParameters() != null ) {
1199-
typeParameters.putAll( typeDefinition.getParameters() );
1200-
}
1201-
}
1202-
else {
1203-
// it could be an unqualified class name, in which case we should qualify
1204-
// it with the implicit package name for this context, if one.
1205-
typeName = mappingDocument.qualifyClassName( typeName );
1206-
}
1207-
}
1208-
if ( source.getTypeInformation().getParameters() != null ) {
1209-
typeParameters.putAll( source.getTypeInformation().getParameters() );
1210-
}
1211-
1212-
binding.setTypeName( typeName );
1213-
binding.setTypeParameters( typeParameters );
1178+
bindCollectionType( mappingDocument, source, binding );
12141179

12151180
final var fetchCharacteristics = source.getFetchCharacteristics();
1216-
if ( fetchCharacteristics.getFetchTiming() == FetchTiming.DELAYED ) {
1217-
binding.setLazy( true );
1218-
binding.setExtraLazy( fetchCharacteristics.isExtraLazy() );
1219-
}
1220-
else {
1221-
binding.setLazy( false );
1222-
}
1181+
binding.setLazy( fetchCharacteristics.getFetchTiming() == FetchTiming.DELAYED );
1182+
binding.setExtraLazy( fetchCharacteristics.isExtraLazy() );
12231183

12241184
setupFetching( source, binding );
12251185

@@ -1230,23 +1190,16 @@ private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttri
12301190
binding.setLoaderName( source.getCustomLoaderName() );
12311191
bindCustomSql( source, binding );
12321192

1233-
if ( source instanceof Sortable sortable ) {
1234-
if ( sortable.isSorted() ) {
1235-
binding.setSorted( true );
1236-
final String comparatorName = sortable.getComparatorName();
1237-
if ( !comparatorName.equals( "natural" ) ) {
1238-
binding.setComparatorClassName( comparatorName );
1239-
}
1240-
}
1241-
else {
1242-
binding.setSorted( false );
1193+
if ( source instanceof Sortable sortable && sortable.isSorted() ) {
1194+
binding.setSorted( true );
1195+
final String comparatorName = sortable.getComparatorName();
1196+
if ( !comparatorName.equals( "natural" ) ) {
1197+
binding.setComparatorClassName( comparatorName );
12431198
}
12441199
}
12451200

1246-
if ( source instanceof Orderable orderable ) {
1247-
if ( orderable.isOrdered() ) {
1248-
binding.setOrderBy( orderable.getOrder() );
1249-
}
1201+
if ( source instanceof Orderable orderable && orderable.isOrdered() ) {
1202+
binding.setOrderBy( orderable.getOrder() );
12501203
}
12511204

12521205
final String cascadeStyle = source.getCascadeStyleName();
@@ -1265,6 +1218,41 @@ private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttri
12651218
}
12661219
}
12671220

1221+
private static void bindCollectionType(
1222+
MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) {
1223+
// bind the collection type info
1224+
final String explicitTypeName = source.getTypeInformation().getName();
1225+
final Map<String,String> typeParameters = new HashMap<>();
1226+
final String typeName;
1227+
if ( explicitTypeName != null ) {
1228+
// see if there is a corresponding type-def
1229+
final var typeDefinition =
1230+
mappingDocument.getMetadataCollector()
1231+
.getTypeDefinition( explicitTypeName );
1232+
if ( typeDefinition != null ) {
1233+
typeName = typeDefinition.getTypeImplementorClass().getName();
1234+
final var parameters = typeDefinition.getParameters();
1235+
if ( parameters != null ) {
1236+
typeParameters.putAll( parameters );
1237+
}
1238+
}
1239+
else {
1240+
// it could be an unqualified class name, in which case qualify
1241+
// it with the implicit package name for this context, if one.
1242+
typeName = mappingDocument.qualifyClassName( explicitTypeName );
1243+
}
1244+
}
1245+
else {
1246+
typeName = null;
1247+
}
1248+
final var parameters = source.getTypeInformation().getParameters();
1249+
if ( parameters != null ) {
1250+
typeParameters.putAll( parameters );
1251+
}
1252+
binding.setTypeName( typeName );
1253+
binding.setTypeParameters( typeParameters );
1254+
}
1255+
12681256
private static void bindCustomSql(PluralAttributeSource source, Collection binding) {
12691257
if ( source.getCustomSqlInsert() != null ) {
12701258
binding.setCustomSQLInsert(
@@ -1298,26 +1286,19 @@ private static void bindCustomSql(PluralAttributeSource source, Collection bindi
12981286

12991287
private static void setupFetching(PluralAttributeSource source, Collection binding) {
13001288
final var fetchCharacteristics = source.getFetchCharacteristics();
1301-
switch ( fetchCharacteristics.getFetchStyle() ) {
1302-
case SELECT:
1303-
binding.setFetchMode( FetchMode.SELECT );
1304-
break;
1305-
case JOIN:
1306-
binding.setFetchMode( FetchMode.JOIN );
1307-
break;
1289+
final var fetchStyle = fetchCharacteristics.getFetchStyle();
1290+
binding.setFetchMode( switch ( fetchStyle ) {
1291+
case SELECT, BATCH, SUBSELECT -> FetchMode.SELECT;
1292+
case JOIN -> FetchMode.JOIN;
1293+
} );
1294+
switch ( fetchStyle ) {
13081295
case BATCH:
1309-
binding.setFetchMode( FetchMode.SELECT );
13101296
binding.setBatchSize( fetchCharacteristics.getBatchSize() );
13111297
break;
13121298
case SUBSELECT:
1313-
binding.setFetchMode( FetchMode.SELECT );
13141299
binding.setSubselectLoadable( true );
1315-
// todo : this could totally be done using a "symbol map" approach
13161300
binding.getOwner().setSubselectLoadableCollections( true );
13171301
break;
1318-
default:
1319-
throw new AssertionFailure( "Unexpected FetchStyle : "
1320-
+ fetchCharacteristics.getFetchStyle().name() );
13211302
}
13221303
}
13231304

@@ -2565,17 +2546,18 @@ public TypeResolution(String typeName, Map<String,String> parameters) {
25652546
private static TypeResolution resolveType(
25662547
MappingDocument sourceDocument,
25672548
HibernateTypeSource typeSource) {
2568-
if ( StringHelper.isEmpty( typeSource.getName() ) ) {
2549+
final String typeSourceName = typeSource.getName();
2550+
if ( StringHelper.isEmpty( typeSourceName ) ) {
25692551
return null;
25702552
}
25712553

25722554
final var typeDefinition =
25732555
sourceDocument.getMetadataCollector()
2574-
.getTypeDefinition( typeSource.getName() );
2556+
.getTypeDefinition( typeSourceName );
25752557
final Map<String,String> typeParameters = new HashMap<>();
25762558
final String typeName;
25772559
if ( typeDefinition == null ) {
2578-
typeName = typeSource.getName();
2560+
typeName = typeSourceName;
25792561
}
25802562
else {
25812563
// the explicit name referred to a type-def
@@ -2585,13 +2567,11 @@ private static TypeResolution resolveType(
25852567
typeParameters.putAll( parameters );
25862568
}
25872569
}
2588-
25892570
// parameters on the property mapping should override parameters in the type-def
25902571
final var parameters = typeSource.getParameters();
25912572
if ( parameters != null ) {
25922573
typeParameters.putAll( parameters );
25932574
}
2594-
25952575
return new TypeResolution( typeName, typeParameters );
25962576
}
25972577

0 commit comments

Comments
 (0)