使用关联模型/实体的多对多

时间:2016-12-06 14:12:00

标签: hibernate jpa spring-data spring-data-jpa

我使用三个模型实体成功地映射了多个关系,如上所示:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private int id;
}

@Entity
public class Task {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private int id;

    private Calendar initialDate;
    private Calendar finalDate;
}

@Entity
public class Member {
    @ManyToOne(fetch=FetchType.EAGER, optional=false)
    @JoinColumn(name = "user_id", nullable = false)//idk if mappedBy is better or right choice against name="user_id"
    private User user;

    @ManyToOne(fetch=FetchType.EAGER, optional=false)
    @JoinColumn(name = "task_id", nullable = false)
    private Task task;
}

问题是我检索了一个任务而我没有得到与之相关的用户列表。如果我在我的任务实体上映射集/集合:

@OneToMany(mappedBy="usuario", fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
private List<Member> members = new ArrayList<>();//I can use HashSet too.

我首先检索的不是用户列表,检索到的对象是循环的(指向自身&#39;永远&#39;)。

我想要做的是在检索任何Task对象时检索用户列表/集合。有小费吗? 感谢

0 个答案:

没有答案