如何使用 JPA、Hibernate 与同一实体建立关系

发布于 2025-01-02 14:18:09 字数 1087 浏览 1 评论 0原文

我正在尝试在不同的人之间创建关系,但找不到如何使用 JPA 来做到这一点的方法。下面是支持该要求的模型:

人员表:
ID 名字 姓氏
1 约翰         海勒
2 约瑟夫·海勒
3 安德鲁·海勒
4 史蒂文      海勒

Person_Relationship表
ID Person1 Person2 关系
1 1           2             父级
2 2           1             孩子
3 1           3 &n bsp;         兄弟姐妹
4 3          1 &n bsp;         兄弟姐妹
5 4          1   ;          秘书

如果您曾经使用 Hibernate 作为 JPA 提供者实现过上述内容,有人可以分享一下您的经验吗?

I am trying to create relationships between different persons but couldn't find a way on how to do it using JPA. Below is the model given to support the requirement:

Person table:
Id FirstName LastName
1 John          Heller
2 Joseph      Heller
3 Andrew      Heller
4 Steven      Heller

Person_Relationship table
Id Person1 Person2 Relationship
1 1             2             Parent
2 2             1             Child
3 1             3             Sibling
4 3             1             Sibling
5 4             1             Secretary

Can someone please share your experience if you have ever implemented the above using Hibernate as JPA provider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗心遗梦遗幸福 2025-01-09 14:18:09

最简单的方法是在 Person 实体和 RelationShip 实体之间使用 OneToMany 关联,每个实体映射关联的表:

public class Person {
    @OneToMany(mappedBy = "person1")
    private List<RelationShip> relationships;

    public List<Person> getSiblings() {
        List<Person> result = new ArrayList<Person>();
        for (RelationShip r : relationShips) {
            if (r.getType() == RelationshipType.SIBLING) {
                result.add(r.getPerson2());
            }
        }
    }

    ...
}

The easiest way is to use a OneToMany association between a Person entity and a RelationShip entity, each entity mapping the associated table:

public class Person {
    @OneToMany(mappedBy = "person1")
    private List<RelationShip> relationships;

    public List<Person> getSiblings() {
        List<Person> result = new ArrayList<Person>();
        for (RelationShip r : relationShips) {
            if (r.getType() == RelationshipType.SIBLING) {
                result.add(r.getPerson2());
            }
        }
    }

    ...
}
强者自强 2025-01-09 14:18:09

使用可连接的标准多对多关系。

Standard many-to-many relationship using a jointable.

马蹄踏│碎落叶 2025-01-09 14:18:09

试试这个。

@Entity
public class Person {

    @Id
    private Long id;

    @OneToMany
    Set<Sibling> siblings;

    @OneToMany
    Set<Parent> parents;

    @OneToMany
    Set<Child> children;

    @OneToMany
    Set<Secretary> secretaries;
}

@Entity
@Table(name="person_relationship")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="relationship", discriminatorType=DiscriminatorType.STRING)
public abstract class Relationship {

    @Id
    private Long id;

    @OneToOne
    @JoinColumn(name="person1")
    private Person owner;

    @OneToOne
    @JoinColumn(name="person2")
    private Person related;
}

@Entity
@DiscriminatorValue("Sibling")
public class Sibling extends Relationship {}

@Entity
@DiscriminatorValue("Child")
public class Child extends Relationship {}

@Entity
@DiscriminatorValue("Parent")
public class Parent extends Relationship {}

@Entity
@DiscriminatorValue("Secretary")
public class Secretary extends Relationship {}

使用它将使 Hibernate (JPA) 完成区分不同类型关系的艰苦工作。

现实生活要是这么简单就好了! ;-)

Try this.

@Entity
public class Person {

    @Id
    private Long id;

    @OneToMany
    Set<Sibling> siblings;

    @OneToMany
    Set<Parent> parents;

    @OneToMany
    Set<Child> children;

    @OneToMany
    Set<Secretary> secretaries;
}

@Entity
@Table(name="person_relationship")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="relationship", discriminatorType=DiscriminatorType.STRING)
public abstract class Relationship {

    @Id
    private Long id;

    @OneToOne
    @JoinColumn(name="person1")
    private Person owner;

    @OneToOne
    @JoinColumn(name="person2")
    private Person related;
}

@Entity
@DiscriminatorValue("Sibling")
public class Sibling extends Relationship {}

@Entity
@DiscriminatorValue("Child")
public class Child extends Relationship {}

@Entity
@DiscriminatorValue("Parent")
public class Parent extends Relationship {}

@Entity
@DiscriminatorValue("Secretary")
public class Secretary extends Relationship {}

Using this will let Hibernate (JPA) do the hard work of discriminating between the different types of relationship.

If only real life were this easy! ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文