【发布时间】:2016-01-26 12:16:19
【问题描述】:
当我尝试访问分离实体的惰性 @ManyToOne 引用的 ID 时,我正面临 LazyInitializationException。我不想完全获取引用,而只需要 ID(它应该存在于原始对象中以便以惰性/延迟方式获取引用)。
EntityA ea = dao.find(1) // find is @Transactional, but transaction is closed after method exits
ea.getLazyReference().getId() // here is get exception. lazyReference is a ManyToOne relation and so the foreight key is stored in EntityA side.
解释一下,如何在不实际获取整个 LazyReference 的情况下访问 LazyReference 的 ID(它实际上存在于 EntityA 的初始选择中)?
【问题讨论】:
-
感谢您的 dup 建议。顺便说一句,您提到的问题可能无法用我自己的问题清楚地回答任何人(尽管回答了我的问题)。答案与 LazyInitializationException 无关,因此人们在搜索此类问题时可能很难找到它。请在此处再次回答 (@Access),建议另一个问题。
-
不客气。完成,请在下面查看我的答案。
标签: java hibernate lazy-loading many-to-one lazy-initialization