8000 move away from stringifying CascadeTypes · hibernate/hibernate-orm@d97029f · GitHub
[go: up one dir, main page]

Skip to content

Commit d97029f

Browse files
committed
move away from stringifying CascadeTypes
1 parent 4616ca3 commit d97029f

File tree

7 files changed

+71
-111
lines changed

7 files changed

+71
-111
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
@@ -908,26 +908,29 @@ public static FetchMode getFetchMode(FetchType fetch) {
908908
};
909909
}
910910

911-
public static String getCascadeStrategy(
912-
jakarta.persistence.CascadeType[] ejbCascades,
913-
Cascade hibernateCascadeAnnotation,
911+
public static EnumSet<CascadeType> aggregateCascadeTypes(
912+
jakarta.persistence.CascadeType[] cascadeTypes,
913+
Cascade cascadeAnnotation,
914914
boolean orphanRemoval,
915915
MetadataBuildingContext context) {
916-
final EnumSet<CascadeType> cascadeTypes = convertToHibernateCascadeType( ejbCascades );
916+
final EnumSet<CascadeType> cascades =
917+
convertToHibernateCascadeType( cascadeTypes );
917918
final CascadeType[] hibernateCascades =
918-
hibernateCascadeAnnotation == null ? null : hibernateCascadeAnnotation.value();
919+
cascadeAnnotation == null
920+
? null
921+
: cascadeAnnotation.value();
919922
if ( !isEmpty( hibernateCascades ) ) {
920-
addAll( cascadeTypes, hibernateCascades );
F438 923+
addAll( cascades, hibernateCascades );
921924
}
922925
if ( orphanRemoval ) {
923-
cascadeTypes.add( CascadeType.DELETE_ORPHAN );
924-
cascadeTypes.add( CascadeType.REMOVE );
926+
cascades.add( CascadeType.DELETE_ORPHAN );
927+
cascades.add( CascadeType.REMOVE );
925928
}
926-
if ( cascadeTypes.contains( CascadeType.REPLICATE ) ) {
929+
if ( cascades.contains( CascadeType.REPLICATE ) ) {
927930
warnAboutDeprecatedCascadeType( CascadeType.REPLICATE );
928931
}
929-
cascadeTypes.addAll( context.getEffectiveDefaults().getDefaultCascadeTypes() );
930-
return renderCascadeTypeList( cascadeTypes );
932+
cascades.addAll( context.getEffectiveDefaults().getDefaultCascadeTypes() );
933+
return cascades;
931934
}
932935

933936
private static EnumSet<CascadeType> convertToHibernateCascadeType(jakarta.persistence.CascadeType[] cascades) {
@@ -951,7 +954,7 @@ private static CascadeType convertCascadeType(jakarta.persistence.CascadeType ca
951954
};
952955
}
953956

954-
private static String renderCascadeTypeList(EnumSet<CascadeType> cascadeTypes) {
957+
public static String renderCascadeTypeList(EnumSet<CascadeType> cascadeTypes) {
955958
final StringBuilder cascade = new StringBuilder();
956959
for ( CascadeType cascadeType : cascadeTypes ) {
957960
cascade.append( "," );

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

Lines changed: 16 additions & 75 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;
@@ -17,57 +18,7 @@
1718
import org.hibernate.AssertionFailure;
1819
import org.hibernate.FetchMode;
1920
import org.hibernate.MappingException;
20-
import org.hibernate.annotations.Bag;
21-
import org.hibernate.annotations.Cache;
22-
import org.hibernate.annotations.CacheLayout;
23-
import org.hibernate.annotations.Cascade;
24-
import org.hibernate.annotations.Check;
25-
import org.hibernate.annotations.Checks;
26-
import org.hibernate.annotations.CollectionId;
27-
import org.hibernate.annotations.CollectionIdJavaType;
28-
import org.hibernate.annotations.CollectionIdJdbcType;
29-
import org.hibernate.annotations.CollectionIdJdbcTypeCode;
30-
import org.hibernate.annotations.CollectionType;
31-
import org.hibernate.annotations.Columns;
32-
import org.hibernate.annotations.CompositeType;
33-
import org.hibernate.annotations.Fetch;
34-
import org.hibernate.annotations.FetchProfileOverride;
35-
import org.hibernate.annotations.Filter;
36-
import org.hibernate.annotations.FilterJoinTable;
37-
import org.hibernate.annotations.Formula;
38-
import org.hibernate.annotations.HQLSelect;
39-
import org.hibernate.annotations.Immutable;
40-
import org.hibernate.annotations.LazyGroup;
41-
import org.hibernate.annotations.ListIndexBase;
42-
import org.hibernate.annotations.ListIndexJavaType;
43-
import org.hibernate.annotations.ListIndexJdbcType;
44-
import org.hibernate.annotations.ListIndexJdbcTypeCode;
45-
import org.hibernate.annotations.ManyToAny;
46-
import org.hibernate.annotations.MapKeyJavaType;
47-
import org.hibernate.annotations.MapKeyJdbcType;
48-
import org.hibernate.annotations.MapKeyJdbcTypeCode;
49-
import org.hibernate.annotations.MapKeyMutability;
50-
import org.hibernate.annotations.MapKeyType;
51-
import org.hibernate.annotations.NotFound;
52-
import org.hibernate.annotations.NotFoundAction;
53-
import org.hibernate.annotations.OnDelete;
54-
import org.hibernate.annotations.OnDeleteAction;
55-
import org.hibernate.annotations.OptimisticLock;
56-
import org.hibernate.annotations.Parameter;
57-
import org.hibernate.annotations.QueryCacheLayout;
58-
import org.hibernate.annotations.SQLDelete;
59-
import org.hibernate.annotations.SQLDeleteAll;
60-
import org.hibernate.annotations.SQLInsert;
61-
import org.hibernate.annotations.SQLJoinTableRestriction;
62-
import org.hibernate.annotations.SQLOrder;
63-
import org.hibernate.annotations.SQLRestriction;
64-
import org.hibernate.annotations.SQLSelect;
65-
import org.hibernate.annotations.SQLUpdate;
66-
import org.hibernate.annotations.SoftDelete;
67-
import org.hibernate.annotations.SortComparator;
68-
import org.hibernate.annotations.SortNatural;
69-
import org.hibernate.annotations.SqlFragmentAlias;
70-
import org.hibernate.annotations.Synchronize;
21+
import org.hibernate.annotations.*;
7122
import org.hibernate.boot.model.IdentifierGeneratorDefinition;
7223
import org.hibernate.boot.models.JpaAnnotations;
7324
import org.hibernate.boot.models.annotations.internal.JoinColumnJpaAnnotation;
@@ -139,6 +90,7 @@
13990
import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;
14091
import static jakarta.persistence.ConstraintMode.PROVIDER_DEFAULT;
14192
import static jakarta.persistence.FetchType.LAZY;
93+
import static org.hibernate.annotations.CascadeType.DELETE_ORPHAN;
14294
import static org.hibernate.boot.model.internal.AnnotatedClassType.EMBEDDABLE;
14395
import static org.hibernate.boot.model.internal.AnnotatedClassType.NONE;
14496
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildColumnFromAnnotation;
@@ -147,11 +99,11 @@
14799
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildFormulaFromAnnotation;
148100
import static org.hibernate.boot.model.internal.AnnotatedJoinColumns.buildJoinColumnsWithDefaultColumnSuffix;
149101
import static org.hibernate.boot.model.internal.AnnotatedJoinColumns.buildJoinTableJoinColumns;
102+
import static org.hibernate.boot.model.internal.BinderHelper.aggregateCascadeTypes;
150103
import static org.hibernate.boot.model.internal.BinderHelper.buildAnyValue;
151104
import static org.hibernate.boot.model.internal.BinderHelper.checkMappedByType;
152105
import static org.hibernate.boot.model.internal.BinderHelper.createSyntheticPropertyReference;
153106
import static org.hibernate.boot.model.internal.BinderHelper.extractFromPackage;
154-
import static org.hibernate.boot.model.internal.BinderHelper.getCascadeStrategy;
155107
import static org.hibernate.boot.model.internal.BinderHelper.getFetchMode;
156108
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
157109
import static org.hibernate.boot.model.internal.BinderHelper.isDefault;
@@ -202,7 +154,7 @@ public abstract class CollectionBinder {
202154
protected MemberDetails property;
203155
private TypeDetails collectionElementType;
204156
private TypeDetails targetEntity;
205-
private String cascadeStrategy;
157+
private EnumSet<CascadeType> cascadeTypes;
206158
private String cacheConcurrencyStrategy;
207159
private String cacheRegionName;
208160
private CacheLayout queryCacheLayout;
@@ -514,12 +466,9 @@ private static String handleTargetEntity(
514466
collectionBinder.setFkJoinColumns( joinColumns );
515467
mappedBy = nullIfEmpty( oneToManyAnn.mappedBy() );
516468
collectionBinder.setTargetEntity( oneToManyAnn.targetEntity() );
517-
collectionBinder.setCascadeStrategy( getCascadeStrategy(
518-
oneToManyAnn.cascade(),
519-
hibernateCascade,
520-
oneToManyAnn.orphanRemoval(),
521-
context
522-
) );
469+
collectionBinder.setCascadeStrategy(
470+
aggregateCascadeTypes( oneToManyAnn.cascade(), hibernateCascade,
471+
oneToManyAnn.orphanRemoval(), context ) );
523472
collectionBinder.setOneToMany( true );
524473
}
525474
else if ( elementCollectionAnn != null ) {
@@ -535,23 +484,15 @@ else if ( elementCollectionAnn != null ) {
535484
else if ( manyToManyAnn != null ) {
536485
mappedBy = nullIfEmpty( manyToManyAnn.mappedBy() );
537486
collectionBinder.setTargetEntity( manyToManyAnn.targetEntity() );
538-
collectionBinder.setCascadeStrategy( getCascadeStrategy(
539-
manyToManyAnn.cascade(),
540-
hibernateCascade,
541-
false,
542-
context
543-
) );
487+
collectionBinder.setCascadeStrategy(
488+
aggregateCascadeTypes( manyToManyAnn.cascade(), hibernateCascade, false, context ) );
544489
collectionBinder.setOneToMany( false );
545490
}
546491
else if ( property.hasDirectAnnotationUsage( ManyToAny.class ) ) {
547492
mappedBy = null;
548493
collectionBinder.setTargetEntity( ClassDetails.VOID_CLASS_DETAILS );
549-
collectionBinder.setCascadeStrategy( getCascadeStrategy(
550-
null,
551-
hibernateCascade,
552-
false,
553-
context
554-
) );
494+
collectionBinder.setCascadeStrategy(
495+
aggregateCascadeTypes( null, hibernateCascade, false, context ) );
555496
collectionBinder.setOneToMany( false );
556497
}
557498
else {
@@ -807,8 +748,8 @@ private void setInsertable(boolean insertable) {
807748
this.insertable = insertable;
808749
}
809750

810-
private void setCascadeStrategy(String cascadeStrategy) {
811-
this.cascadeStrategy = cascadeStrategy;
751+
private void setCascadeStrategy(EnumSet<CascadeType> cascadeTypes) {
752+
this.cascadeTypes = cascadeTypes;
812753
}
813754

814755
private void setAccessType(AccessType accessType) {
@@ -1267,8 +1208,8 @@ private void bindProperty() {
12671208
PropertyBinder binder = new PropertyBinder();
12681209
binder.setName( propertyName );
12691210
binder.setValue( collection );
1270-
binder.setCascade( cascadeStrategy );
1271-
if ( cascadeStrategy != null && cascadeStrategy.contains( "delete-orphan" ) ) {
1211+
binder.setCascade( cascadeTypes );
1212+
if ( cascadeTypes != null && cascadeTypes.contains( DELETE_ORPHAN ) ) {
12721213
collection.setOrphanDelete( true );
12731214
}
12741215
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 );

0 commit comments

Comments
 (0)
0