HHH-15099 - Improve handling of associations marked with @NotFound

- Disable physical foreign-key export for `@NotFound` mappings
This commit is contained in:
Steve Ebersole 2022-03-03 15:45:12 -06:00
parent 50c0c2ff9d
commit ec737a7f15
2 changed files with 19 additions and 14 deletions

View File

@ -3961,10 +3961,20 @@ public final class AnnotationBinder {
JoinColumns joinColumns, JoinColumns joinColumns,
MetadataBuildingContext context) { MetadataBuildingContext context) {
final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault(); final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault();
if ( ( joinColumn != null && ( joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT final NotFound notFoundAnn= property.getAnnotation( NotFound.class );
|| joinColumn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) )
|| ( joinColumns != null && ( joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT if ( notFoundAnn != null ) {
|| joinColumns.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) ) { // supersedes all others
value.disableForeignKey();
}
else if ( joinColumn != null && (
joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT
|| ( joinColumn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) ) {
value.disableForeignKey();
}
else if ( joinColumns != null && (
joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT
|| ( joinColumns.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) ) {
value.disableForeignKey(); value.disableForeignKey();
} }
else { else {

View File

@ -7,6 +7,11 @@
package org.hibernate.orm.test.annotations.notfound; package org.hibernate.orm.test.annotations.notfound;
import java.io.Serializable; import java.io.Serializable;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction; import org.hibernate.annotations.NotFoundAction;
@ -16,15 +21,6 @@ import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
/** /**
@ -92,7 +88,6 @@ public class NotFoundLogicalOneToOneTest {
} }
@OneToOne(fetch = FetchType.LAZY) @OneToOne(fetch = FetchType.LAZY)
@JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
@NotFound(action = NotFoundAction.IGNORE) @NotFound(action = NotFoundAction.IGNORE)
public Currency getCurrency() { public Currency getCurrency() {
return currency; return currency;