Hibernate继承单table
Hibernate inheritance single table
我正在尝试使用数据库 table "persons".
实现单个 table Hibernate 继承策略
我有一个家庭,有一个丈夫,一个妻子,一份child人的名单和一份其他犯人的名单。每个人可能是许多家庭的成员(即丈夫可能是一个家庭的丈夫,另一个家庭的child)。
@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 是所有人员的共同 class:
@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 和其他以及 Parent class 是相同的:
@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 {}
现在在系统启动过程中出现以下问题:
org.hibernate.MappingException: Repeated column in mapping for entity: org.parafia.model.Other column: type (should be mapped with insert="false" update="false")
我该怎么办?我的模型有意义吗?
此致
Does my model make sense?
它不是:如果你的目标是,比如说,爱丽丝是某个家庭的母亲,也是其他家庭的 child,那么爱丽丝在 persons
是? Alice 应该是 Parent
还是 Child
的实例?
子类必须是分离集。
我正在尝试使用数据库 table "persons".
实现单个 table Hibernate 继承策略我有一个家庭,有一个丈夫,一个妻子,一份child人的名单和一份其他犯人的名单。每个人可能是许多家庭的成员(即丈夫可能是一个家庭的丈夫,另一个家庭的child)。
@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 是所有人员的共同 class:
@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 和其他以及 Parent class 是相同的:
@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 {}
现在在系统启动过程中出现以下问题:
org.hibernate.MappingException: Repeated column in mapping for entity: org.parafia.model.Other column: type (should be mapped with insert="false" update="false")
我该怎么办?我的模型有意义吗?
此致
Does my model make sense?
它不是:如果你的目标是,比如说,爱丽丝是某个家庭的母亲,也是其他家庭的 child,那么爱丽丝在 persons
是? Alice 应该是 Parent
还是 Child
的实例?
子类必须是分离集。