HHH-18129 don't force cascade=PERSIST for @MapsId fields

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-05-16 20:07:38 +02:00 committed by Steve Ebersole
parent a76a4a585b
commit e5b6b456f9
16 changed files with 46 additions and 63 deletions

View File

@ -41,8 +41,7 @@ public class AnyBinder {
boolean isIdentifierMapper,
MetadataBuildingContext context,
MemberDetails property,
AnnotatedJoinColumns joinColumns,
boolean forcePersist) {
AnnotatedJoinColumns joinColumns) {
//check validity
if ( property.hasAnnotationUsage( Columns.class ) ) {
@ -66,7 +65,7 @@ public class AnyBinder {
}
}
bindAny(
getCascadeStrategy( null, hibernateCascade, false, forcePersist, context ),
getCascadeStrategy( null, hibernateCascade, false, context ),
//@Any has no cascade attribute
joinColumns,
onDeleteAnn == null ? null : onDeleteAnn.getEnum( "action" ),

View File

@ -937,7 +937,6 @@ public class BinderHelper {
List<jakarta.persistence.CascadeType> ejbCascades,
AnnotationUsage<Cascade> hibernateCascadeAnnotation,
boolean orphanRemoval,
boolean forcePersist,
MetadataBuildingContext context) {
final EnumSet<CascadeType> cascadeTypes = convertToHibernateCascadeType( ejbCascades );
final List<CascadeType> hibernateCascades = hibernateCascadeAnnotation == null
@ -950,9 +949,6 @@ public class BinderHelper {
cascadeTypes.add( CascadeType.DELETE_ORPHAN );
cascadeTypes.add( CascadeType.REMOVE );
}
if ( forcePersist ) {
cascadeTypes.add( CascadeType.PERSIST );
}
cascadeTypes.addAll( context.getEffectiveDefaults().getDefaultCascadeTypes() );
return renderCascadeTypeList( cascadeTypes );
}

View File

@ -366,10 +366,6 @@ public abstract class CollectionBinder {
collectionBinder.bind();
}
private static TypeDetails determineElementType(PropertyData inferredData) {
return inferredData.getClassOrElementType();
}
private static NotFoundAction notFoundAction(
PropertyHolder propertyHolder,
PropertyData inferredData,
@ -511,7 +507,6 @@ public abstract class CollectionBinder {
oneToManyAnn.getList( "cascade" ),
hibernateCascade,
oneToManyAnn.getBoolean( "orphanRemoval" ),
false,
context
) );
collectionBinder.setOneToMany( true );
@ -534,7 +529,6 @@ public abstract class CollectionBinder {
manyToManyAnn.getList( "cascade" ),
hibernateCascade,
false,
false,
context
) );
collectionBinder.setOneToMany( false );
@ -546,7 +540,6 @@ public abstract class CollectionBinder {
null,
hibernateCascade,
false,
false,
context
) );
collectionBinder.setOneToMany( false );

View File

@ -862,8 +862,7 @@ public class PropertyBinder {
context,
property,
columnsBuilder.getJoinColumns(),
propertyBinder,
isForcePersist( property )
propertyBinder
);
}
else if ( isOneToOne( property ) ) {
@ -875,8 +874,7 @@ public class PropertyBinder {
context,
property,
columnsBuilder.getJoinColumns(),
propertyBinder,
isForcePersist( property )
propertyBinder
);
}
else if ( isAny( property ) ) {
@ -888,8 +886,7 @@ public class PropertyBinder {
isIdentifierMapper,
context,
property,
columnsBuilder.getJoinColumns(),
isForcePersist( property )
columnsBuilder.getJoinColumns()
);
}
else if ( isCollection( property ) ) {
@ -952,11 +949,6 @@ public class PropertyBinder {
|| property.hasAnnotationUsage( ManyToAny.class );
}
private static boolean isForcePersist(MemberDetails property) {
return property.hasAnnotationUsage( MapsId.class )
|| property.hasAnnotationUsage( Id.class );
}
private static void bindVersionProperty(
PropertyHolder propertyHolder,
PropertyData inferredData,

View File

@ -82,8 +82,7 @@ public class ToOneBinder {
MetadataBuildingContext context,
MemberDetails property,
AnnotatedJoinColumns joinColumns,
PropertyBinder propertyBinder,
boolean forcePersist) {
PropertyBinder propertyBinder) {
final AnnotationUsage<ManyToOne> manyToOne = property.getAnnotationUsage( ManyToOne.class );
//check validity
@ -109,7 +108,7 @@ public class ToOneBinder {
final AnnotationUsage<OnDelete> onDelete = property.getAnnotationUsage( OnDelete.class );
final AnnotationUsage<JoinTable> joinTable = propertyHolder.getJoinTable( property );
bindManyToOne(
getCascadeStrategy( manyToOne.getList( "cascade" ), hibernateCascade, false, forcePersist, context ),
getCascadeStrategy( manyToOne.getList( "cascade" ), hibernateCascade, false, context ),
joinColumns,
joinTable,
!isMandatory( manyToOne.getBoolean( "optional" ), property, notFoundAction ),
@ -219,7 +218,7 @@ public class ToOneBinder {
joinColumns.setMapsId( mapsId.getString( "value" ) );
}
boolean hasSpecjManyToOne = handleSpecjSyntax( joinColumns, inferredData, context, property );
final boolean hasSpecjManyToOne = handleSpecjSyntax( joinColumns, inferredData, context, property );
value.setTypeName( inferredData.getClassOrElementName() );
final String propertyName = inferredData.getPropertyName();
value.setTypeUsingReflection( propertyHolder.getClassName(), propertyName );
@ -454,8 +453,7 @@ public class ToOneBinder {
MetadataBuildingContext context,
MemberDetails property,
AnnotatedJoinColumns joinColumns,
PropertyBinder propertyBinder,
boolean forcePersist) {
PropertyBinder propertyBinder) {
final AnnotationUsage<OneToOne> oneToOne = property.getAnnotationUsage( OneToOne.class );
//check validity
@ -486,7 +484,7 @@ public class ToOneBinder {
final AnnotationUsage<OnDelete> onDelete = property.getAnnotationUsage( OnDelete.class );
final AnnotationUsage<JoinTable> joinTable = propertyHolder.getJoinTable( property );
bindOneToOne(
getCascadeStrategy( oneToOne.getList( "cascade" ), hibernateCascade, oneToOne.getBoolean( "orphanRemoval" ), forcePersist, context ),
getCascadeStrategy( oneToOne.getList( "cascade" ), hibernateCascade, oneToOne.getBoolean( "orphanRemoval" ), context ),
joinColumns,
joinTable,
!isMandatory( oneToOne.getBoolean( "optional" ), property, notFoundAction ),

View File

@ -5,6 +5,7 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.annotations.derivedidentities.e1.b;
import jakarta.persistence.CascadeType;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
@ -21,7 +22,7 @@ public class Dependent {
//@JoinColumn(name="FK") // id attribute mapped by join column default
@MapsId("empPK") // maps empPK attribute of embedded id
@ManyToOne
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(nullable=false)
Employee emp;

View File

@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import jakarta.persistence.CascadeType;
import org.hibernate.Hibernate;
import org.hibernate.jpa.SpecHints;
@ -204,7 +205,7 @@ public class IdClassEntityGraphTest {
public static class Child {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "parent_id")
private Parent parent;

View File

@ -302,7 +302,7 @@ public class RefreshTest {
public static class RealmAttributeEntity {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;

View File

@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import jakarta.persistence.CascadeType;
import org.hibernate.Hibernate;
import org.hibernate.jpa.SpecHints;
@ -198,7 +199,7 @@ public class IdClassEntityGraphTest {
public static class Child {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "parent_id")
private Parent parent;

View File

@ -7,6 +7,8 @@
package org.hibernate.orm.test.id;
import java.io.Serializable;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
@ -78,7 +80,7 @@ public class IdClassManyToOneCascadeTest extends BaseEntityManagerFunctionalTest
private long id;
@Id
@ManyToOne
@ManyToOne(cascade = CascadeType.PERSIST)
private ReferencedEntity referencedEntity;
public ReferencedEntity getReferencedEntity() {

View File

@ -500,7 +500,7 @@ public class OptionalEagerNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@Fetch(FetchMode.JOIN)
@ -531,7 +531,7 @@ public class OptionalEagerNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@Fetch(FetchMode.SELECT)
@ -562,7 +562,7 @@ public class OptionalEagerNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "fk", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@ -593,7 +593,7 @@ public class OptionalEagerNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "fk", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))

View File

@ -707,7 +707,7 @@ public class OptionalEagerRefNonPKNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@Fetch(FetchMode.JOIN)
@JoinColumn(
@ -741,7 +741,7 @@ public class OptionalEagerRefNonPKNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@Fetch(FetchMode.JOIN)
@ -776,7 +776,7 @@ public class OptionalEagerRefNonPKNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@Fetch(FetchMode.SELECT)
@JoinColumn(
@ -810,7 +810,7 @@ public class OptionalEagerRefNonPKNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@Fetch(FetchMode.SELECT)
@ -845,7 +845,7 @@ public class OptionalEagerRefNonPKNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(
name = "cityName",
@ -879,7 +879,7 @@ public class OptionalEagerRefNonPKNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(
@ -914,7 +914,7 @@ public class OptionalEagerRefNonPKNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(
name = "cityName",
@ -948,7 +948,7 @@ public class OptionalEagerRefNonPKNotFoundTest {
@Id
private Long id;
@OneToOne
@OneToOne(cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(

View File

@ -490,7 +490,7 @@ public class OptionalLazyNotFoundTest {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private City city;
@ -519,7 +519,7 @@ public class OptionalLazyNotFoundTest {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@ -549,7 +549,7 @@ public class OptionalLazyNotFoundTest {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private City city;
@ -578,7 +578,7 @@ public class OptionalLazyNotFoundTest {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@ -608,7 +608,7 @@ public class OptionalLazyNotFoundTest {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(name = "fk", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private City city;
@ -637,7 +637,7 @@ public class OptionalLazyNotFoundTest {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "fk", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@ -667,7 +667,7 @@ public class OptionalLazyNotFoundTest {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(name = "fk", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private City city;
@ -696,7 +696,7 @@ public class OptionalLazyNotFoundTest {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "fk", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))

View File

@ -319,7 +319,7 @@ public class RequiredLazyNotFoundTest {
@Id
private Long id;
@OneToOne(optional = false, fetch = FetchType.LAZY)
@OneToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private City city;
@ -348,7 +348,7 @@ public class RequiredLazyNotFoundTest {
@Id
private Long id;
@OneToOne(optional = false, fetch = FetchType.LAZY)
@OneToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private City city;
@ -377,7 +377,7 @@ public class RequiredLazyNotFoundTest {
@Id
private Long id;
@OneToOne(optional = false, fetch = FetchType.LAZY)
@OneToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(name = "fk", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private City city;
@ -406,7 +406,7 @@ public class RequiredLazyNotFoundTest {
@Id
private Long id;
@OneToOne(optional = false, fetch = FetchType.LAZY)
@OneToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@MapsId
@JoinColumn(name = "fk", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private City city;

View File

@ -172,7 +172,7 @@ public class JPARefreshTest {
public static class RealmAttributeEntity {
@Id
@ManyToOne(fetch= FetchType.LAZY)
@ManyToOne(fetch= FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;

View File

@ -205,7 +205,7 @@ public class RefreshTest {
public static class RealmAttributeEntity {
@Id
@ManyToOne(fetch= FetchType.LAZY)
@ManyToOne(fetch= FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "REALM_ID")
protected RealmEntity realm;