gpt4 book ai didi

Jpa OneToMany 有条件

转载 作者:行者123 更新时间:2023-12-03 23:31:34 33 4
gpt4 key购买 nike

我有2张 table :

第一个是“人”:

  • person_id,
  • 人名

  • 第二个是“PersonsGraphs”:
  • person_id1,
  • person_id2,
  • 关系类型

  • 我正在寻找一种建立“家谱”的方法。

    我的第一个选择是 : 将 personGraphs 加载到 HashTable 中,然后递归构建树。

    我想出的第二个选项 :使用 @OneToMany jpa-relation .这可以工作,但有时我有一些 relation_types我想要/不想包括的。是否有任何选项可以让我在 @OneToMany 上设置一些条件?使用时的关系 @JoinTable ?

    谢谢!
    橡木

    最佳答案

    尝试使用 Hibernate @Where annotation , 例如:

    @Entity
    public class Person {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    @Enumerated(EnumType.STRING)
    private Gender gender;

    @ManyToOne
    private Person parent;

    @Where(clause = "gender = 'MALE'")
    @OneToMany(mappedBy = "person")
    private List<Person> sons;

    @Where(clause = "gender = 'FEMALE'")
    @OneToMany(mappedBy = "person")
    private List<Person> daughters;
    }

    public enum Gender {
    MALE, FEMALE
    }

    关于Jpa OneToMany 有条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16324100/

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