JPA 和 Hibernate Fetch 忽略关联?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 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.