How to disable Hibernate foreign key constraint for bidirectional communication?

I am trying to disable a foreign key constraint generated in my bidirectional association. I was able to do this for all my unidirectional associations, but for some reason it doesn't work here.

I know about the error with ContraintMode.NO_CONSTRAINT, which was recently fixed in Hibernate 5.x, and I am running the latest version of Hibernate 5.2.6.

My annotations now look like this:

class Parent {
  @OneToMany(mappedBy="parent", cascade=CascadeType.ALL, orphanRemoval=true)
  @OrderColumn(name="childIndex")
  public List<Child> getChildren() {
    return children;
  }
}

class Child {
  @ManyToOne(optional=false)
  @JoinColumn(name="parent", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
  public Parent getParent() {
    return parent;
  }
}

But despite NO_CONSTRAINT, Hibernate still creates a foreign key constraint for child.parent -> parent.id.

Is there anything extra I need to do to suppress a foreign key for a bidirectional case?

Thank!

+6
1

Hibernate, . Https://hibernate.atlassian.net/browse/HHH-8805

, @org.hibernate.annotations.ForeignKey(name = "none") .

class Parent {

  @OneToMany(mappedBy="parent", cascade=CascadeType.ALL, orphanRemoval=true)
  @OrderColumn(name="childIndex")
  @org.hibernate.annotations.ForeignKey(name = "none")
  public List<Child> getChildren() {
    return children;
  }

}

: , JPA 2.1 javax.persistence.ForeignKey. .

+3

Source: https://habr.com/ru/post/1667184/


All Articles