Skip to content

Commit b96ec13

Browse files
committed
move away from stringifying CascadeTypes
1 parent 5b69a92 commit b96ec13

File tree

7 files changed

+70
-60
lines changed

7 files changed

+70
-60
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
*/
55
package org.hibernate.boot.model.internal;
66

7+
import java.util.EnumSet;
78
import java.util.Locale;
89

910
import org.hibernate.AnnotationException;
1011
import org.hibernate.AssertionFailure;
1112
import org.hibernate.annotations.AnyDiscriminator;
1213
import org.hibernate.annotations.AnyDiscriminatorImplicitValues;
1314
import org.hibernate.annotations.Cascade;
15+
import org.hibernate.annotations.CascadeType;
1416
import org.hibernate.annotations.Columns;
1517
import org.hibernate.annotations.Formula;
1618
import org.hibernate.annotations.OnDelete;
@@ -29,7 +31,7 @@
2931
import jakarta.persistence.FetchType;
3032
import jakarta.persistence.JoinTable;
3133

32-
import static org.hibernate.boot.model.internal.BinderHelper.getCascadeStrategy;
34+
import static org.hibernate.boot.model.internal.BinderHelper.aggregateCascadeTypes;
3335
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverridableAnnotation;
3436
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
3537

@@ -67,7 +69,7 @@ static void bindAny(
6769
}
6870
}
6971
bindAny(
70-
getCascadeStrategy( null, hibernateCascade, false, context ),
72+
aggregateCascadeTypes( null, hibernateCascade, false, context ),
7173
//@Any has no cascade attribute
7274
joinColumns,
7375
onDeleteAnn == null ? null : onDeleteAnn.action(),
@@ -81,7 +83,7 @@ static void bindAny(
8183
}
8284

8385
private static void bindAny(
84-
String cascadeStrategy,
86+
EnumSet<CascadeType> cascadeStrategy,
8587
AnnotatedJoinColumns columns,
8688
OnDeleteAction onDeleteAction,
8789
Nullability nullability,

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -902,26 +902,29 @@ public static FetchMode getFetchMode(FetchType fetch) {
902902
};
903903
}
904904

905-
public static String getCascadeStrategy(
906-
jakarta.persistence.CascadeType[] ejbCascades,
907-
Cascade hibernateCascadeAnnotation,
905+
public static EnumSet<CascadeType> aggregateCascadeTypes(
906+
jakarta.persistence.CascadeType[] cascadeTypes,
907+
Cascade cascadeAnnotation,
908908
boolean orphanRemoval,
909909
MetadataBuildingContext context) {
910-
final EnumSet<CascadeType> cascadeTypes = convertToHibernateCascadeType( ejbCascades );
910+
final EnumSet<CascadeType> cascades =
911+
convertToHibernateCascadeType( cascadeTypes );
911912
final CascadeType[] hibernateCascades =
912-
hibernateCascadeAnnotation == null ? null : hibernateCascadeAnnotation.value();
913+
cascadeAnnotation == null
914+
? null
915+
: cascadeAnnotation.value();
913916
if ( !isEmpty( hibernateCascades ) ) {
914-
addAll( cascadeTypes, hibernateCascades );
917+
addAll( cascades, hibernateCascades );
915918
}
916919
if ( orphanRemoval ) {
917-
cascadeTypes.add( CascadeType.DELETE_ORPHAN );
918-
cascadeTypes.add( CascadeType.REMOVE );
920+
cascades.add( CascadeType.DELETE_ORPHAN );
921+
cascades.add( CascadeType.REMOVE );
919922
}
920-
if ( cascadeTypes.contains( CascadeType.REPLICATE ) ) {
923+
if ( cascades.contains( CascadeType.REPLICATE ) ) {
921924
warnAboutDeprecatedCascadeType( CascadeType.REPLICATE );
922925
}
923-
cascadeTypes.addAll( context.getEffectiveDefaults().getDefaultCascadeTypes() );
924-
return renderCascadeTypeList( cascadeTypes );
926+
cascades.addAll( context.getEffectiveDefaults().getDefaultCascadeTypes() );
927+
return cascades;
925928
}
926929

927930
private static EnumSet<CascadeType> convertToHibernateCascadeType(jakarta.persistence.CascadeType[] cascades) {
@@ -945,7 +948,7 @@ private static CascadeType convertCascadeType(jakarta.persistence.CascadeType ca
945948
};
946949
}
947950

948-
private static String renderCascadeTypeList(EnumSet<CascadeType> cascadeTypes) {
951+
public static String renderCascadeTypeList(EnumSet<CascadeType> cascadeTypes) {
949952
final StringBuilder cascade = new StringBuilder();
950953
for ( CascadeType cascadeType : cascadeTypes ) {
951954
cascade.append( "," );

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.lang.annotation.Annotation;
88
import java.util.Comparator;
9+
import java.util.EnumSet;
910
import java.util.HashMap;
1011
import java.util.List;
1112
import java.util.Locale;
@@ -89,6 +90,7 @@
8990
import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;
9091
import static jakarta.persistence.ConstraintMode.PROVIDER_DEFAULT;
9192
import static jakarta.persistence.FetchType.LAZY;
93+
import static org.hibernate.annotations.CascadeType.DELETE_ORPHAN;
9294
import static org.hibernate.boot.model.internal.AnnotatedClassType.EMBEDDABLE;
9395
import static org.hibernate.boot.model.internal.AnnotatedClassType.NONE;
9496
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildColumnFromAnnotation;
@@ -97,11 +99,11 @@
9799
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildFormulaFromAnnotation;
98100
import static org.hibernate.boot.model.internal.AnnotatedJoinColumns.buildJoinColumnsWithDefaultColumnSuffix;
99101
import static org.hibernate.boot.model.internal.AnnotatedJoinColumns.buildJoinTableJoinColumns;
102+
import static org.hibernate.boot.model.internal.BinderHelper.aggregateCascadeTypes;
100103
import static org.hibernate.boot.model.internal.BinderHelper.buildAnyValue;
101104
import static org.hibernate.boot.model.internal.BinderHelper.checkMappedByType;
102105
import static org.hibernate.boot.model.internal.BinderHelper.createSyntheticPropertyReference;
103106
import static org.hibernate.boot.model.internal.BinderHelper.extractFromPackage;
104-
import static org.hibernate.boot.model.internal.BinderHelper.getCascadeStrategy;
105107
import static org.hibernate.boot.model.internal.BinderHelper.getFetchMode;
106108
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
107109
import static org.hibernate.boot.model.internal.BinderHelper.isDefault;
@@ -152,7 +154,7 @@ public abstract class CollectionBinder {
152154
protected MemberDetails property;
153155
private TypeDetails collectionElementType;
154156
private TypeDetails targetEntity;
155-
private String cascadeStrategy;
157+
private EnumSet<CascadeType> cascadeTypes;
156158
private String cacheConcurrencyStrategy;
157159
private String cacheRegionName;
158160
private CacheLayout queryCacheLayout;
@@ -459,12 +461,9 @@ private static String handleTargetEntity(
459461
collectionBinder.setFkJoinColumns( joinColumns );
460462
mappedBy = nullIfEmpty( oneToManyAnn.mappedBy() );
461463
collectionBinder.setTargetEntity( oneToManyAnn.targetEntity() );
462-
collectionBinder.setCascadeStrategy( getCascadeStrategy(
463-
oneToManyAnn.cascade(),
464-
hibernateCascade,
465-
oneToManyAnn.orphanRemoval(),
466-
context
467-
) );
464+
collectionBinder.setCascadeStrategy(
465+
aggregateCascadeTypes( oneToManyAnn.cascade(), hibernateCascade,
466+
oneToManyAnn.orphanRemoval(), context ) );
468467
collectionBinder.setOneToMany( true );
469468
}
470469
else if ( elementCollectionAnn != null ) {
@@ -480,23 +479,15 @@ else if ( elementCollectionAnn != null ) {
480479
else if ( manyToManyAnn != null ) {
481480
mappedBy = nullIfEmpty( manyToManyAnn.mappedBy() );
482481
collectionBinder.setTargetEntity( manyToManyAnn.targetEntity() );
483-
collectionBinder.setCascadeStrategy( getCascadeStrategy(
484-
manyToManyAnn.cascade(),
485-
hibernateCascade,
486-
false,
487-
context
488-
) );
482+
collectionBinder.setCascadeStrategy(
483+
aggregateCascadeTypes( manyToManyAnn.cascade(), hibernateCascade, false, context ) );
489484
collectionBinder.setOneToMany( false );
490485
}
491486
else if ( property.hasDirectAnnotationUsage( ManyToAny.class ) ) {
492487
mappedBy = null;
493488
collectionBinder.setTargetEntity( ClassDetails.VOID_CLASS_DETAILS );
494-
collectionBinder.setCascadeStrategy( getCascadeStrategy(
495-
null,
496-
hibernateCascade,
497-
false,
498-
context
499-
) );
489+
collectionBinder.setCascadeStrategy(
490+
aggregateCascadeTypes( null, hibernateCascade, false, context ) );
500491
collectionBinder.setOneToMany( false );
501492
}
502493
else {
@@ -752,8 +743,8 @@ private void setInsertable(boolean insertable) {
752743
this.insertable = insertable;
753744
}
754745

755-
private void setCascadeStrategy(String cascadeStrategy) {
756-
this.cascadeStrategy = cascadeStrategy;
746+
private void setCascadeStrategy(EnumSet<CascadeType> cascadeTypes) {
747+
this.cascadeTypes = cascadeTypes;
757748
}
758749

759750
private void setAccessType(AccessType accessType) {
@@ -1213,8 +1204,8 @@ private void bindProperty() {
12131204
PropertyBinder binder = new PropertyBinder();
12141205
binder.setName( propertyName );
12151206
binder.setValue( collection );
1216-
binder.setCascade( cascadeStrategy );
1217-
if ( cascadeStrategy != null && cascadeStrategy.contains( "delete-orphan" ) ) {
1207+
binder.setCascade( cascadeTypes );
1208+
if ( cascadeTypes != null && cascadeTypes.contains( DELETE_ORPHAN ) ) {
12181209
collection.setOrphanDelete( true );
12191210
}
12201211
binder.setLazy( collection.isLazy() );

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
*/
55
package org.hibernate.boot.model.internal;
66

7+
import java.util.EnumSet;
78
import java.util.Map;
89

910
import org.hibernate.AnnotationException;
1011
import org.hibernate.MappingException;
12+
import org.hibernate.annotations.CascadeType;
1113
import org.hibernate.annotations.LazyGroup;
1214
import org.hibernate.annotations.NotFoundAction;
1315
import org.hibernate.annotations.OnDeleteAction;
@@ -23,6 +25,7 @@
2325
import org.hibernate.mapping.PersistentClass;
2426
import org.hibernate.mapping.Property;
2527
import org.hibernate.mapping.SortableValue;
28+
import org.hibernate.mapping.Value;
2629
import org.hibernate.models.spi.MemberDetails;
2730
import org.hibernate.type.ForeignKeyDirection;
2831

@@ -49,7 +52,7 @@ public class OneToOneSecondPass implements SecondPass {
4952
private final NotFoundAction notFoundAction;
5053
private final OnDeleteAction onDeleteAction;
5154
private final boolean optional;
52-
private final String cascadeStrategy;
55+
private final EnumSet<CascadeType> cascadeStrategy;
5356
private final AnnotatedJoinColumns joinColumns;
5457
private final MetadataBuildingContext buildingContext;
5558
private final String referencedEntityName;
@@ -65,7 +68,7 @@ public OneToOneSecondPass(
6568
NotFoundAction notFoundAction,
6669
OnDeleteAction onDeleteAction,
6770
boolean optional,
68-
String cascadeStrategy,
71+
EnumSet<CascadeType> cascadeStrategy,
6972
AnnotatedJoinColumns columns,
7073
MetadataBuildingContext buildingContext) {
7174
this.ownerEntity = ownerEntity;
@@ -84,11 +87,9 @@ public OneToOneSecondPass(
8487

8588
@Override
8689
public void doSecondPass(Map<String, PersistentClass> persistentClasses) throws MappingException {
87-
final OneToOne value = new OneToOne(
88-
buildingContext,
89-
propertyHolder.getTable(),
90-
propertyHolder.getPersistentClass()
91-
);
90+
final OneToOne value =
91+
new OneToOne( buildingContext, propertyHolder.getTable(),
92+
propertyHolder.getPersistentClass() );
9293
final String propertyName = inferredData.getPropertyName();
9394
value.setPropertyName( propertyName );
9495
value.setReferencedEntityName( referencedEntityName );
@@ -100,7 +101,9 @@ public void doSecondPass(Map<String, PersistentClass> persistentClasses) throws
100101

101102
value.setConstrained( !optional );
102103
value.setForeignKeyType( getForeignKeyDirection() );
103-
bindForeignKeyNameAndDefinition( value, property, property.getDirectAnnotationUsage( ForeignKey.class ), buildingContext );
104+
bindForeignKeyNameAndDefinition( value, property,
105+
property.getDirectAnnotationUsage( ForeignKey.class ),
106+
buildingContext );
104107

105108
final PropertyBinder binder = new PropertyBinder();
106109
binder.setName( propertyName );
@@ -144,10 +147,11 @@ private void bindUnowned(Map<String, PersistentClass> persistentClasses, OneToOn
144147
+ "' targets the type '" + targetEntityName + "'" + problem );
145148
}
146149
final Property targetProperty = targetProperty( oneToOne, targetEntity );
147-
if ( targetProperty.getValue() instanceof OneToOne ) {
150+
final Value targetPropertyValue = targetProperty.getValue();
151+
if ( targetPropertyValue instanceof OneToOne ) {
148152
propertyHolder.addProperty( property, inferredData.getAttributeMember(), inferredData.getDeclaringClass() );
149153
}
150-
else if ( targetProperty.getValue() instanceof ManyToOne ) {
154+
else if ( targetPropertyValue instanceof ManyToOne ) {
151155
bindTargetManyToOne( persistentClasses, oneToOne, property, targetEntity, targetProperty );
152156
}
153157
else {
@@ -158,7 +162,7 @@ else if ( targetProperty.getValue() instanceof ManyToOne ) {
158162
}
159163
checkMappedByType(
160164
mappedBy,
161-
targetProperty.getValue(),
165+
targetPropertyValue,
162166
oneToOne.getPropertyName(),
163167
propertyHolder,
164168
persistentClasses

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.hibernate.MappingException;
3333
import org.hibernate.annotations.Any;
3434
import org.hibernate.annotations.AttributeBinderType;
35+
import org.hibernate.annotations.CascadeType;
3536
import org.hibernate.annotations.CompositeType;
3637
import org.hibernate.annotations.IdGeneratorType;
3738
import org.hibernate.annotations.Immutable;
@@ -122,7 +123,7 @@ public class PropertyBinder {
122123
private Component componentElement;
123124
private boolean insertable = true;
124125
private boolean updatable = true;
125-
private String cascade;
126+
private EnumSet<CascadeType> cascadeTypes;
126127
private BasicValueBinder basicValueBinder;
127128
private ClassDetails declaringClass;
128129
private boolean declaringClassSet;
@@ -199,8 +200,8 @@ private void setComponentElement(Component componentElement) {
199200
this.componentElement = componentElement;
200201
}
201202

202-
public void setCascade(String cascadeStrategy) {
203-
this.cascade = cascadeStrategy;
203+
public void setCascade(EnumSet<CascadeType> cascadeTypes) {
204+
this.cascadeTypes = cascadeTypes;
204205
}
205206

206207
public void setBuildingContext(MetadataBuildingContext buildingContext) {
@@ -438,7 +439,7 @@ public Property makeProperty() {
438439
property.setValue( value );
439440
property.setLazy( lazy );
440441
property.setLazyGroup( lazyGroup );
441-
property.setCascade( cascade );
442+
property.setCascade( cascadeTypes );
442443
property.setPropertyAccessorName( accessType.getType() );
443444
property.setReturnedClassName( returnedClassName );
444445
// property.setPropertyAccessStrategy( propertyAccessStrategy );

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
package org.hibernate.boot.model.internal;
66

77
import java.util.ArrayList;
8+
import java.util.EnumSet;
89
import java.util.List;
910

1011
import org.hibernate.AnnotationException;
1112
import org.hibernate.AssertionFailure;
1213
import org.hibernate.FetchMode;
1314
import org.hibernate.annotations.Cascade;
15+
import org.hibernate.annotations.CascadeType;
1416
import org.hibernate.annotations.Columns;
1517
import org.hibernate.annotations.Fetch;
1618
import org.hibernate.annotations.FetchProfileOverride;
@@ -46,7 +48,7 @@
4648

4749
import static jakarta.persistence.FetchType.EAGER;
4850
import static jakarta.persistence.FetchType.LAZY;
49-
import static org.hibernate.boot.model.internal.BinderHelper.getCascadeStrategy;
51+
import static org.hibernate.boot.model.internal.BinderHelper.aggregateCascadeTypes;
5052
import static org.hibernate.boot.model.internal.BinderHelper.getFetchMode;
5153
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
5254
import static org.hibernate.boot.model.internal.BinderHelper.isDefault;
@@ -103,7 +105,7 @@ && isIdentifier( propertyHolder, propertyBinder, isIdentifierMapper ) ) {
103105
final OnDelete onDelete = property.getDirectAnnotationUsage( OnDelete.class );
104106
final JoinTable joinTable = propertyHolder.getJoinTable( property );
105107
bindManyToOne(
106-
getCascadeStrategy( manyToOne.cascade(), hibernateCascade, false, context ),
108+
aggregateCascadeTypes( manyToOne.cascade(), hibernateCascade, false, context ),
107109
joinColumns,
108110
joinTable,
109111
!isMandatory( manyToOne.optional(), property, notFoundAction ),
@@ -144,7 +146,7 @@ private static boolean isMandatory(boolean optional, MemberDetails property, Not
144146
}
145147

146148
private static void bindManyToOne(
147-
String cascadeStrategy,
149+
EnumSet<CascadeType> cascadeStrategy,
148150
AnnotatedJoinColumns joinColumns,
149151
JoinTable joinTable,
150152
boolean optional,
@@ -256,7 +258,7 @@ static boolean isTargetAnnotatedEntity(ClassDetails targetEntity, MemberDetails
256258
}
257259

258260
private static void processManyToOneProperty(
259-
String cascadeStrategy,
261+
EnumSet<CascadeType> cascadeStrategy,
260262
AnnotatedJoinColumns columns,
261263
boolean optional,
262264
PropertyData inferredData,
@@ -426,7 +428,7 @@ static void bindOneToOne(
426428
final OnDelete onDelete = property.getDirectAnnotationUsage( OnDelete.class );
427429
final JoinTable joinTable = propertyHolder.getJoinTable( property );
428430
bindOneToOne(
429-
getCascadeStrategy( oneToOne.cascade(), hibernateCascade, oneToOne.orphanRemoval(), context ),
431+
aggregateCascadeTypes( oneToOne.cascade(), hibernateCascade, oneToOne.orphanRemoval(), context ),
430432
joinColumns,
431433
joinTable,
432434
!isMandatory( oneToOne.optional(), property, notFoundAction ),
@@ -447,7 +449,7 @@ static void bindOneToOne(
447449
}
448450

449451
private static void bindOneToOne(
450-
String cascadeStrategy,
452+
EnumSet<CascadeType> cascadeStrategy,
451453
AnnotatedJoinColumns joinColumns,
452454
JoinTable joinTable,
453455
boolean optional,

0 commit comments

Comments
 (0)