gpt4 book ai didi

java - Hibernate继承单表

转载 作者:行者123 更新时间:2023-12-02 11:36:04 25 4
gpt4 key购买 nike

我正在尝试使用数据库表“persons”实现单表 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 是所有人的公共(public)类:

@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;
}

子类、其他类和父类是相同的:

@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 ,那么 type 应该做什么?爱丽丝在persons中的行是? Alice 应该是 Parent 的实例吗?或Child

子类必须是分离集。

关于java - Hibernate继承单表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48924354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com