2013-05-01 143 views
1

我有2个表:JPA一对多与条件

首先是 “人”:

  • 为person_id,
  • PERSON_NAME

第二个是 “PersonsGraphs”:

  • person_id1,
  • person_id2,
  • relation_type

我正在寻找一种方式来建立一个 “家谱”。

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

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

谢谢! 橡树

+1

附注:我认为应该将一个表命名为Person和其他人员图。 – 2013-05-02 05:40:36

+0

同意:) – oak 2013-05-02 10:38:18

回答

1

我建议创建一个关系类来建模连接表。

人 - 一对多 - 关系 - 关系 - 多对一源 - 多对一目标

在EclipseLink的,你可以添加一个表达式标准没有任何关系的映射,

http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

+0

嘿@詹姆斯,是否有可能在没有EclipseLink的情况下实现相同的结果? – oak 2013-06-08 18:12:29

0

尝试使用Hibernate @Whereannotation,例如:

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