gpt4 book ai didi

java - Hibernate 映射 - 如何连接三个表

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:14:54 25 4
gpt4 key购买 nike

<分区>

我有 3 个实体:PERSON、CAR 和 PROFFESION。

    @Entity
@Table(name = "PERSON")
public class Person implements Serializable {

@Id
private Long id;

@OneToMany(mappedBy = "person", fetch = FetchType.EAGER)
private List<Car> cars;

@OneToMany(mappedBy = "person", fetch = FetchType.EAGER)
private List<Profession> professions;
}
    @Entity
@Table(name = "PROFESSION")
public class Profession implements Serializable {

@Id
private Long id;

@ManyToOne
@JoinColumn(name = "PERSON_ID")
private Person person;
}
    @Entity
@Table(name = "CAR")
public class Car implements Serializable {

@Id
private Long id;

@ManyToOne
@JoinColumn(name = "PERSON_ID")
private Person person;
}

当我试图检索与两个职业和一辆汽车有联系的人时,结果是两个职业和两辆重复的汽车。

或者如果他连接了五辆汽车和一个职业,结果是五辆汽车和五个重复的职业。

我应该如何更正映射才能收到正确的结果?

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