java - 如何使用 Hibernate 注释获取孙子表?

标签 java hibernate hibernate-mapping hibernate-annotations

我有 3 个表,因为 LetterOfCredit 有许多 ProformatInvoice,其中有许多 PurchasingOrder。

我想在我的实体 LetterOfCredit 中获取通过 ProformatInvoice 链接的所有采购订单的列表。

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

你能告诉我我做错了什么吗?

最佳答案

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.

关于java - 如何使用 Hibernate 注释获取孙子表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33498613/

相关文章:

java - 无法在 Eclipse Neon 上安装颠覆性连接器 (SVN)

java - 为什么需要为synchronized语句指定锁?

java - 异常 : org. hibernate.exception.SQLGrammerException 和 org.hibernate.exception.GenericJDBCEXception

java - hibernate 工具: Mappings and Annotations Fail

java - 无法在hibernate中加载hbm文件

java - @OneToOne 和 @JoinColumn 只保存 UUID 而不是整个对象

java - HTTPServletRequest.getParameterNames() 的顺序与 HTML 表单中不同

java - JOOQ 代码生成器跳过包含 JsonNode 字段的类

hibernate - 使用 2 个实体和带有 group by 的聚合函数时,如何处理 JPA 中的类型查询?

Hibernate Search + Infinispan + S3 -- 防止字母数字文件名