2015-11-03 50 views
1

我有3個表格,因爲LetterOfCredit有許多具有許多PurchaseOrder的ProformatInvoice。如何使用Hibernate註釋獲取大型子表?

我想在我的實體LetterOfCredit中獲取通過ProformatInvoice鏈接的所有PurchaseOrder的列表。

在SQL它看起來像:

SELECT * 
FROM purchase_order 
JOIN proformat_invoice pi ON pi.id = pi_id 
JOIN letter_of_credit lc ON lc.id = pi.lc_id 
WHERE lc_id = 3; 

但在LetterOfCredit.java,我試圖用proformat_invoice作爲jointable但我得到一個空列表....

@OneToMany(fetch = FetchType.LAZY) 
@JoinTable(
     name="proformat_invoice", 
     joinColumns = @JoinColumn(name="lc_id", referencedColumnName = "id"), 
     inverseJoinColumns = @JoinColumn(name="id", referencedColumnName = "pi_id") 
) 
private List<PurchaseOrder> purchaseOrders; 

莫非你告訴我我做錯了什麼?

回答

0
We can fetch the grand children efficiently by using hibernate annotation ---- @OneToMany(mappedBy = "category", fetch = FetchType.LAZY). 
So in this case this will not give an exception if its unable to fetch the sub children till session is open. 
We can manually pull the sub children by using the getters in our code without getting any exception , as we have already told the compiler that it will be lazy loading because there are child elements and there sub child elements. 
+0

@OneToMany(的mappedBy = 「proformatInvoice」,取= FetchType.LAZY) 私人列表 purchaseOrders中;給我一個空列表 – Labe

+0

您的評論似乎來自一篇文章或文檔。我可以有鏈接嗎? – Labe

+0

其評論我的話,沒有任何鏈接。 – arjun9