How do I disable Hibernate foreign key constraint on a bidirectional association?
Addition to the @Bustanil Arifin answer:
You can combine @OneToMany
and @javax.persistence.ForeignKey
in next way:
class Parent {
@OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name = "parent", foreignKey = @javax.persistence.ForeignKey(name = "none"))
public List<Child> getChildren() {
return children;
}
}
This is known issue in Hibernate, see https://hibernate.atlassian.net/browse/HHH-8805
Solution is to add @org.hibernate.annotations.ForeignKey(name = "none") on the mapped side.
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;
}
}
Note: Prefer the JPA 2.1 introduced javax.persistence.ForeignKey
instead. The native annotation is deprecated.