| Consider the following minimal example: Parent
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
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
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
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
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. |