ui:repeat 内的 ui:repeat 和 LazyInitException

发布于 2024-08-23 17:27:24 字数 1754 浏览 9 评论 0原文

我目前遇到了 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 技术交流群。

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

发布评论

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

评论(1

蒲公英的约定 2024-08-30 17:27:24

你们的实体经理工厂叫什么名字?

来自 OpenEntityManagerInView 文档:

在 Spring 的根 Web 应用程序上下文中查找 EntityManagerFactory。支持 web.xml 中的“entityManagerFactoryBeanName”过滤器 init-param;默认 bean 名称是“entityManagerFactory”。

如果您指定的名称与“entityManagerFactory”不同,则过滤器看不到您的工厂。

What is the name of your entity manager factory?

From OpenEntityManagerInView docs:

Looks up the EntityManagerFactory in Spring's root web application context. Supports a "entityManagerFactoryBeanName" filter init-param in web.xml; the default bean name is "entityManagerFactory".

If you specified the name different from "entityManagerFactory" then the filter doesn't see your factory.

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