hbm2ddl - How to avoid that Hibernate is creating Foreign Key constraints?

若如初见. 提交于 2021-01-28 07:44:03

问题


Dear all I am using Hibernate with hbm2ddl. I don't like that for one relationship foreign key constraints get created. Unfortunately I could not achieve it so far. I tried it with Hibernate and JPA annotations, had no luck. Any hints?

I am using Hibernate 4.3.1 and mysql 5.6

@Entity
class Artikel {
   ...
   @OneToMany(fetch=FetchType.LAZY, mappedBy="artikel")
   @NotFound(action=NotFoundAction.IGNORE)
   private List<Bild> images;
}

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
@Entity
class Bild {
  @ManyToOne(fetch=FetchType.LAZY)
  @org.hibernate.annotations.ForeignKey(name = "none") 
  @JoinColumn(name="idKey",  insertable=false, updatable=false, 
    foreignKey = @ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT)) 
  private Artikel artikel;
}

It always creates the foreign key: FK_k93uxcqtc87jifh2j3rliumuj for table "bild" on column "idKey": Error during import: "Cannot add or update a child row: a foreign key constraint fails (skateshop.bild, CONSTRAINT FK_k93uxcqtc87jifh2j3rliumuj FOREIGN KEY (idKey) REFERENCES artikel (id))"


回答1:


You are using the correct annotation:

@ForeignKey(value = ConstraintMode.NO_CONSTRAINT)

It looks like a bug in hibernate 4.x that is now fixed in 5.x. If you could upgrade to Hibernate 5.x, that would solve the issue.

See also JPA association without foreign key



来源:https://stackoverflow.com/questions/32154592/hbm2ddl-how-to-avoid-that-hibernate-is-creating-foreign-key-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!