JPA 和 Hibernate Fetch 忽略关联?

发布于 2024-07-13 18:05:42 字数 360 浏览 6 评论 0原文

我的 JPA 实体(对象 A)在 ArrayList 中具有一对多拥有关系(对象 B)。

我希望能够查询(Hibernate 或 JPA)对象 A,而无需在一对多 ArrayList 中返回关联对象 B 的任何实例(无代理或其他方式)。

理想情况下,返回的 ArrayList 将为 null 或空。

这可能吗? 在粗略的伪代码中,这就是我想要的:

“from ObjectA where ObjectA.id=5 DO NOT INCLUDE ObjectB”

“Select a from ObjectA a FETCH IGNORE a.ObjectBs where a.id=5”

I have JPA entity (Object A) with a One-Many owning relationship (Object B) in an ArrayList.

I want to be able to query (either Hibernate or JPA) for Object A without having any of the instances of association Object B returned (no proxies or otherwise) in the One-Many ArrayList.

Ideally the returned ArrayList would be null or empty.

Is this possible? In rough pseudocode this is what I want:

"from ObjectA where ObjectA.id=5 DO NOT INCLUDE ObjectB"

or

"Select a from ObjectA a FETCH IGNORE a.ObjectBs where a.id=5"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2024-07-20 18:05:42

如果 ObjectA 中 ObjectB 的关联是惰性的,则您的查询不会返回 ObjectB,除非您的查询指定获取它们。

如果您在会话关闭后访问任何 ObjectB,您将收到 LazyInitializationException,因为您的 ObjectB 未加载到内存中。

If the association for ObjectBs in ObjectA is lazy, ObjectBs won't be returned with your query unless your query specifies to fetch them.

If you were to access any ObjectBs once your session is closed, you would get a LazyInitializationException because your ObjectBs were not loaded into memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文