ui:repeat 内的 ui:repeat 和 LazyInitException
我目前遇到了 LazyInitException,页面包含如下代码:
<h:form>
<ui:repeat value="#{searchBean.storiesByTag}" var="iStory">
<ui:repeat value="{iStory.tags}"var="iTag">
<!-- Lazy init exception here -->
#{iTag.content}
</ui:repeat>
</ui:repeat>
</h:form>
storiesByTag()
是一种检索故事 List
的 bean 方法。第二个 ui:repeat 应该获取每个标签并显示其内容。默认情况下,所有获取都是惰性的,以避免加载超出需要的对象。
我对此仍然很模糊,但据我了解,这种情况可能会发生,因为 EntityManager
在请求或 期间关闭(从
正在被访问。@Transactional
云退出) >集合
Spring 的 OpenEntityManagerInViewFilter 已添加到项目中,但我不确定它是否正确执行其工作。欢迎任何对此进行测试的建议。
因为我认为 Collection
是这里的问题,所以我想知道什么是一个好的解决方案。我应该使用 fetch join
修改 DAO 方法吗?或者我应该采取 hacky 路线,使用
触发 bean 内的方法并从数据库中获取一些新的 Tag 对象?
编辑:对于 Bozho:
web.xml:
<!-- Open EM in View Filter -->
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- End -->
对于 Roman:
applicationContext.xml:
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
I'm currently experiencing a LazyInitException with a page containing code like the following:
<h:form>
<ui:repeat value="#{searchBean.storiesByTag}" var="iStory">
<ui:repeat value="{iStory.tags}"var="iTag">
<!-- Lazy init exception here -->
#{iTag.content}
</ui:repeat>
</ui:repeat>
</h:form>
storiesByTag()
is a bean method that retrieves a List
of stories. The second ui:repeat
is supposed to then take each tag and display it's content. All fetching is lazy by default to avoid loading more objects than is necessary.
I'm still fuzzy on this but, from what I understand, this can happen because an EntityManager
is closed (exit from @Transactional
cloud) during a request or a Collection
is being accessed.
Spring's OpenEntityManagerInViewFilter
from Spring has been added to the project but I'm not sure if it's doing it's job correctly. Any suggestions for testing this are welcome.
Since I assume the Collection
is the problem here I'd like to know what would be a good solution. Should I modify a DAO method with a fetch join
? Or should I take the hacky route by using <f:view beforePhaseListener=...>
to trigger a method inside a bean and get some fresh Tag objects from the database?
Edit: For Bozho:
web.xml:
<!-- Open EM in View Filter -->
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- End -->
For Roman:
applicationContext.xml:
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你们的实体经理工厂叫什么名字?
来自 OpenEntityManagerInView 文档:
如果您指定的名称与“entityManagerFactory”不同,则过滤器看不到您的工厂。
What is the name of your entity manager factory?
From OpenEntityManagerInView docs:
If you specified the name different from "entityManagerFactory" then the filter doesn't see your factory.