Hibernate schemaupdate generates duplicate foreign keys

I'm trying to upgrade hibernate 4.2.0 to 4.3.6, but the schemaupdate task doesn't work because it generates duplicate foreign keys with the same name. It seems that the previous version of hibernate also generated duplicate foreign keys, but I didn't notice it because they had different names.

The problem is with the following (simplified) classes

public class Relation
    @ManyToOne(optional = false)
    private Parent parent;

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Parent


public class Child extends Parent
    @OneToMany(mappedBy = "parent")
    private List<Relation> relations


The schemaupdate task generates a foreign key from the relationship to the parent and from the child to the relationship. But it doesn't recognize that it's the same foreign key.

I have come up with three possible solutions, but I hope there is a better one.

  • targetEntity: Add targetEntity = Child.class to Relation class, but Relation.class doesn't know Child class. To fix this, you will create a circular dependency.
  • orm.xml: I could define targetEntity in orm.xml which will work.
  • org.hibernate.annotations.ForeignKey (name = "none") in one of two relationships. This will prevent one foreign key from being created. But this will have implications for other projects using the same class. Also the hibernate version of annotation is deprecated and javax.persistence cannot be used with mapping.

Is there a better way to do this?


source to share

All Articles