I am trying to implement a single table Hibernate inheritance strategy with a DB table "persons".
I have a family which contains a husband, a wife, a list of children and a list of other inmates. Each person may be a member of many families (i.e. a husband may be a husband in one family and a child in other).
@Entity
@Table(name = "families")
public class Family extends BaseObject {
private Parent husband;
private Parent wife;
private String surname;
private List<Child> children = new ArrayList<Child>();
private List<Other> others = new ArrayList<Other>();
@ManyToOne(optional = true, cascade = CascadeType.ALL)
@JoinColumn(name = "husband_id", nullable = true)
public Parent getHusband() {
return husband;
}
@ManyToOne(optional = true, cascade = CascadeType.ALL)
@JoinColumn(name = "wife_id", nullable = true)
public Parent getWife() {
return wife;
}
@LazyCollection(LazyCollectionOption.TRUE)
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="persons_families", joinColumns=@JoinColumn(name="family_id"), inverseJoinColumns=@JoinColumn(name="person_id"))
public List<Other> getOthers() {
return others;
}
@LazyCollection(LazyCollectionOption.TRUE)
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="persons_families", joinColumns=@JoinColumn(name="family_id"), inverseJoinColumns=@JoinColumn(name="person_id"))
public List<Child> getChildren() {
return children;
}
Persons is a common class for all persons:
@Entity
@Table(name = "persons")
@DiscriminatorColumn(name = "type")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Person extends BaseObject {
private List<Family> families;
@ManyToMany(cascade=CascadeType.ALL)
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="persons_families", joinColumns=@JoinColumn(name="person_id"), inverseJoinColumns=@JoinColumn(name="family_id"))
public List<Family> getFamilies() {
return families;
}
Child and Other and Parent classes are identical:
@Entity
@Table(name = "persons")
@DiscriminatorValue(value = "children")
public class Child extends Person {}
@Entity
@Table(name = "persons")
@DiscriminatorValue(value = "others")
public class Other extends Person {}
@Entity
@Table(name = "persons")
@DiscriminatorValue(value = "parents")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Parent extends Person {}
Now during the system startup I have the following problem:
org.hibernate.MappingException: Repeated column in mapping for entity: org.parafia.model.Other column: type (should be mapped with insert="false" update="false")
What could I do about that? Does my model make sense?
Best regards