Skip to content

Commit

Permalink
HHH-15099 - Improve handling of associations marked with @NotFound
Browse files Browse the repository at this point in the history
- Disable physical foreign-key export for `@NotFound` mappings
  • Loading branch information
sebersole committed Mar 3, 2022
1 parent 50c0c2f commit ec737a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Expand Up @@ -3961,10 +3961,20 @@ public static void bindForeignKeyNameAndDefinition(
JoinColumns joinColumns,
MetadataBuildingContext context) {
final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault();
if ( ( joinColumn != null && ( joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT
|| joinColumn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) )
|| ( joinColumns != null && ( joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT
|| joinColumns.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) ) {
final NotFound notFoundAnn= property.getAnnotation( NotFound.class );

if ( notFoundAnn != null ) {
// 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();
}
else {
Expand Down
Expand Up @@ -7,6 +7,11 @@
package org.hibernate.orm.test.annotations.notfound;

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.NotFoundAction;
Expand All @@ -16,15 +21,6 @@
import org.hibernate.testing.orm.junit.SessionFactoryScope;
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;

/**
Expand Down Expand Up @@ -92,7 +88,6 @@ public void setName(String name) {
}

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
@NotFound(action = NotFoundAction.IGNORE)
public Currency getCurrency() {
return currency;
Expand Down

0 comments on commit ec737a7

Please sign in to comment.