2013-12-12 88 views
0

我有一個簡單的@ManyToMany關係,我試圖捕獲,但我一直遇到無限循環問題。春季Roo避免許多週期

我有三個班的一種這樣的:人的問題,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以加載加載加載加載Person的IssuePositions的問題的IssuePositions。

我該如何解決這個問題?我已經去除了取= FetchType.EAGER但這並沒有幫助

回答

0

This教程提供瞭如何使用@ManyToMany註釋使用@JoinTable映射關係一個很好的例子。 Here's一個稍微複雜的例子。

0

我試過你的實現,它工作正常我有以下幾點。

​​

當我嘗試這樣做:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaQuery"); 
     EntityManager em = emf.createEntityManager(); 

     Person p = em.find(Person.class, 1); 
     System.out.println(p); 

我得到這個

休眠:從人選擇person0_.id作爲id1_2_0_ person0_其中 person0_.id =?休眠:選擇issueposit0_.person_id爲 person3_2_2_,issueposit0_.id爲id1_1_2_,issueposit0_.id爲 id1_1_1_,issueposit0_.issue_id爲issue2_1_1_,issueposit0_.person_id 爲person3_1_1_,issue1_.id作爲id1_0_0_從IssuePosition issueposit0_左外連接問題issue1_ issueposit0_.issue_id = issue1_.id where issueposit0_.person_id =? 休眠:選擇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作爲id1_2_0_從IssuePosition issueposit0_ 左外連接的人person1_ on issueposit0_.person_id = person1_.id where issueposit0_.issue_id =?休眠:選擇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作爲id1_2_0_從IssuePosition issueposit0_左外連接的人person1_ issueposit0_.person_id = person1_.id where issueposit0_.issue_id =? 休眠:選擇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作爲id1_2_0_從IssuePosition issueposit0_ 左外連接的人person1_ on issueposit0_.person_id = person1_.id where issueposit0_.issue_id =?休眠:選擇issueposit0_.person_id 爲person3_2_2_,issueposit0_.id爲id1_1_2_,issueposit0_.id爲 id1_1_1_,issueposit0_.issue_id爲issue2_1_1_,issueposit0_.person_id 爲person3_1_1_,issue1_.id作爲id1_0_0_從IssuePosition issueposit0_左外連接問題issue1_上 issueposit0_.issue_id = issue1_.id其中issueposit0_.person_id在 (從IssuePosition選擇person1_.id issueposit0_左外連接 人person1_上issueposit0_.person_id = person1_.id其中 issueposit0_.issue_id =?)

Person [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或其他方法是否不啓動進程來創建循環。

​​