We're updating the issue view to help you get more done. 

Audit table schema generation creates foreign key constraint to Entity table for EmbeddedId containing ManyToOne

Description

Consider the following minimal example:

Parent

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import java.io.Serializable; import java.util.List; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; import org.hibernate.envers.Audited; @Entity @Audited public class Parent implements Serializable { @Id private String id; @OneToMany(mappedBy = "parent") private List<CorrectChild> correctChildren; @OneToMany(mappedBy = "id.parent") private List<IncorrectChild> incorrectChildren; }

CorrectChild

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.io.Serializable; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.ManyToOne; import javax.persistence.MapsId; import org.hibernate.envers.Audited; @Entity @Audited public class CorrectChild implements Serializable { @EmbeddedId private CorrectChildId id; @ManyToOne @MapsId("parent_id") private Parent parent; }

CorrectChildId

1 2 3 4 5 6 7 8 9 10 11 12 import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; @Embeddable public class CorrectChildId implements Serializable { @Column(name = "parent_id") private String id; private Integer number; }

IncorrectChild

1 2 3 4 5 6 7 8 9 10 11 12 import java.io.Serializable; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import org.hibernate.envers.Audited; @Entity @Audited public class IncorrectChild implements Serializable { @EmbeddedId private IncorrectChildId id; }

IncorrectChildId

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import java.io.Serializable; import javax.persistence.Embeddable; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; @Embeddable public class IncorrectChildId implements Serializable { @ManyToOne @JoinColumns({ @JoinColumn(name = "parent_id", referencedColumnName = "id") }) private Parent parent; private Integer number; }

Now, when generating the Envers schema using <property name="hibernate.hbm2ddl.auto" value="create"/>, the audit tables for Parent and CorrectChild are generated correctly. However, the audit table for IncorrectChild is then incorrectly containing a foreign key constraint to Parent.
I assume that Envers uses Hibernate mechanisms to generate DDL and does not cover the case that an @EmbeddedId can contain not only primitive fields but also references to other entities which have to be skipped like any other @OneToOne or @ManyToOne relationship.

Environment

None

Status

Assignee

Chris Cranford

Reporter

Johannes Geiger

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.2.3

Priority

Major