如何使用Hibernate注释获取grand children表?

时间:2015-11-03 12:00:30

标签: java hibernate hibernate-mapping hibernate-annotations

我有3个表,因为LetterOfCredit有许多ProformatInvoice,它有许多PurchaseOrder。

我想在我的实体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作为连接表,但我得到一个空列表....

@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;

你能告诉我我做错了吗?

1 个答案:

答案 0 :(得分: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.