ui:重复一个ui:repeat和LazyInitException [英] ui:repeat inside a ui:repeat and LazyInitException

查看:118
本文介绍了ui:重复一个ui:repeat和LazyInitException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到一个LazyInitException,其中包含以下代码的页面:

 < h:form> 
< ui:repeat value =#{searchBean.storiesByTag}var =iStory>
< ui:repeat value ={iStory.tags}var =iTag>
<! - 这里的Lazy init异常 - >
#{iTag.content}
< / ui:repeat>
< / ui:repeat>
< / h:form>

storiesByTag()是一种bean方法,检索列表的故事。第二个 ui:repeat 应该采取每个标签并显示它的内容。所有提取默认为懒惰,以避免加载比必要的更多的对象。



我仍然模糊,但从我的理解,这可能发生,因为一个 EntityManager 在请求期间关闭(从 @Transactional 云退出)或集合正在被访问。



Spring的来自Spring的OpenEntityManagerInViewFilter 已被添加到项目中,但我不知道它是否正常工作。任何有关测试的建议都是值得欢迎的。



由于我假设集合是我想要的问题知道什么会是一个很好的解决方案。应该使用 fetch join 修改DAO方法吗?或者我应该使用黑客路线,使用< f:view beforePhaseListener = ...> 来触发一个bean内的方法,并从数据库中获取一些新的Tag对象?



编辑:对于Bozho:



web.xml

 <! - 在视图过滤器中打开EM  - > 

< 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>

<! - 结束 - >

对于罗马:



applicationContext.xml

 <! - 单个JPA EntityManagerFactory(替代JTA)的事务管理器 -  - > 
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager
p:entityManagerFactory-ref =entityManagerFactory/>


解决方案

您的实体经理工厂名称是什么? / p>

从OpenEntityManagerInView文档:


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


如果您指定的名称不同于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"/>

解决方案

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.

这篇关于ui:重复一个ui:repeat和LazyInitException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆