我正在尝试使用数据库表“ persons”实现单个表的Hibernate继承策略。

我有一个家庭,其中有丈夫,妻子,孩子清单和其他囚犯清单。每个人可能是许多家庭的成员(即丈夫可能是一个家庭的丈夫,而另一个家庭的孩子)。

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


人是所有人的共同阶级:

@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")


我该怎么办?我的模型有意义吗?

最好的祝福

最佳答案

我的模型有意义吗?


并不是这样的:如果您的目标是,例如,爱丽丝(Alice)在某些家庭中是母亲,而在其他家庭中还是孩子,那么type中Alice所在行的persons应该是什么?爱丽丝应该是Parent还是Child的实例吗?

子类必须是分离集。

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

10-10 11:56