我有一个简单的@ManyToMany关系我想要捕获,但我一直遇到无限循环问题。
我有三个类如下:Person,Issue,IssuePosition
每个人都可以对任何问题持有立场,并且可以针对不同的问题采取立场。职位是“为”“反对”“中立”
问题是“增税”“建立新学校”等问题
现在我进入了Person类:
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "person", fetch = FetchType.EAGER)
@Fetch(FetchMode.SUBSELECT)
private List<IssuePosition> issuePositions = new ArrayList<IssuePosition>();
并在问题类中:
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "issue", fetch = FetchType.EAGER)
private List<IssuePosition> issuePositions = new ArrayList<IssuePosition>();
在IssuePosition类中的:
@ManyToOne(optional = true)
private Issue issue;
@ManyToOne(optional = true)
private Person person;
这会导致调用加载Person以加载IssuePositions,后者会加载载入载有Person的IssuePositions的问题。
如何阻止这种情况?我删除了fetch = FetchType.EAGER,但没有帮助
答案 0 :(得分:0)
答案 1 :(得分:0)
我尝试了你的实现,它运行正常我有以下内容。
@Entity
public class IssuePosition {
@Id
private int id;
@ManyToOne(optional = true)
private Issue issue;
@ManyToOne(optional = true)
private Person person;
@Override
public String toString() {
return "IssuePosition [id=" + id + "]";
}
}
@Entity
public class Person {
@Id
private int id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "person", fetch = FetchType.EAGER)
@Fetch(FetchMode.SUBSELECT)
private List<IssuePosition> issuePositions = new ArrayList<IssuePosition>();
@Override
public String toString() {
return "Person [id=" + id + ", issuePositions=" + issuePositions + "]";
}
}
@Entity
public class Issue {
@Id
private int id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "issue", fetch = FetchType.EAGER)
private List<IssuePosition> issuePositions = new ArrayList<IssuePosition>();
@Override
public String toString() {
return "Issue [id=" + id + ", issuePositions=" + issuePositions + "]";
}
}
当我尝试这个时:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaQuery");
EntityManager em = emf.createEntityManager();
Person p = em.find(Person.class, 1);
System.out.println(p);
我获得了这个
Hibernate:从Person person0_ where中选择person0_.id作为id1_2_0_ person0_.id =? Hibernate:选择issueposit0_.person_id as person3_2_2_,issueposit0_.id为id1_1_2_,issueposit0_.id为 id1_1_1_,issueposit0_.issue_id为issue2_1_1_,issueposit0_.person_id as person3_1_1_,issue1_.id为IssuePosition的id1_0_0_ issueposit0_ left outer join Issue issue1_ on issueposit0_.issue_id = issue1_.id其中issueposit0_.person_id =? Hibernate:选择issueposit0_.issue_id作为issue2_0_2_, issueposit0_.id为id1_1_2_,issueposit0_.id为id1_1_1_, issueposit0_.issue_id为issue2_1_1_,issueposit0_.person_id as person3_1_1_,person1_.id,来自IssuePosition issueposit0_的id1_2_0_ left outer join person person__ on issueposit0_.person_id = person1_.id issueposit0_.issue_id =? Hibernate:选择issueposit0_.issue_id 如issue2_0_2_,issueposit0_.id为id1_1_2_,issueposit0_.id为 id1_1_1_,issueposit0_.issue_id为issue2_1_1_,issueposit0_.person_id 作为person3_1_1_,person1_.id为来自IssuePosition的id1_2_0_ issueposit0_ left outer join Person person1_ on issueposit0_.person_id = person1_.id其中issueposit0_.issue_id =? Hibernate:选择issueposit0_.issue_id作为issue2_0_2_, issueposit0_.id为id1_1_2_,issueposit0_.id为id1_1_1_, issueposit0_.issue_id为issue2_1_1_,issueposit0_.person_id as person3_1_1_,person1_.id,来自IssuePosition issueposit0_的id1_2_0_ left outer join person person__ on issueposit0_.person_id = person1_.id issueposit0_.issue_id =? Hibernate:选择issueposit0_.person_id as person3_2_2_,issueposit0_.id为id1_1_2_,issueposit0_.id as id1_1_1_,issueposit0_.issue_id为issue2_1_1_,issueposit0_.person_id as person3_1_1_,issue1_.id为IssuePosition的id1_0_0_ issueposit0_ left outer join Issue issue1_ on issueposit0_.issue_id = issue1_.id其中,issueposit0_.person_id在 (从IssuePosition issueposit0_ left outer join中选择person1_.id 在personposit0_.person_id = person1_.id所在的人person1_ issueposit0_.issue_id =?)
人[id = 1,issuePositions = [IssuePosition [id = 1,issue = Issue [id = 1]],IssuePosition [id = 2,issue = Issue [id = 2]],IssuePosition [id = 3,issue = Issue [id = 3]]]]
似乎工作正常,你是否使用getter或setter打印东西,这可能会使进程无限循环。检查toString或任何其他方法是否不启动进程来进行循环。