服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

spring在mvc层解决jpa的缓迟加载问题

    作为ejb3.0的一部分,jpa是一个好东西。其简单的配置方式及强大的默认配置支持,使其可以轻松自由的存在于轻量与重量之间,如果现在您的javaee项目,不管是选择轻量级构架还是重量级构架,如果持久层不选择使用jpa,而是用一些orm框架(如hibernate、toplink)的专用api,那么在将来的某一天一定会为这个选择而说出至尊宝那句“假如上天再给我一个机会…”的至理名言。
       下面是一个简单的entity,是对一个cms系统中,关于树状信息目录实体类的定义,包括了一些详细的映射的配置信息。

@entity
public class newsdir ...{
    @id
    @generatedvalue(strategy = generationtype.table)
    private long id;// 主键

    @column(unique = true, nullable = false, length = 16)
    private string sn;// 目录编号

    private string title; // 目录名称

    @onetomany(mappedby = "parent", cascade = javax.persistence.cascadetype.remove)
    private list<newsdir> children = new java.util.arraylist<newsdir>();// 下级目录

    @manytoone
    private newsdir parent;// 父级目录

}
  当然,跟任何其它优秀的技术一样,jpa也不是完美的,在使用的过程中难免都会出这样那样的问题,这就需要我们程序员具有格物致知的本领,在应用中灵活应付这些问题。
  这里例举一个缓迟加载的问题,以上面的新闻目录entity为例。对于parnet与children这个一对多的双向关联,为了提高系统效率,children默认使用的是缓迟加载的方式。在一些轻量级的构架中,由于脱离了j2ee容器及事务支持,经常会出现entity脱离了persitence context,变成了detach或entitymanager关闭,导致一些我们预想中的一些功能无法正常运行。
  最常见的就是在使用mvc框架的时候,在表示层无法加载需要缓迟加载的数据。比如,在一个基于easyjweb的mvc应用中,action中的方法如下:

public page dolist(webform form, module module) ...{
        newsdirqueryobject ndqo = new newsdirqueryobject();
        form.topo(ndqo);
        ndqo.setdel(true);
        ipagelist pagelist = service.querydirsbyconditions(ndqo);
        commutilforteaec.saveipagelist2webform(pagelist, form);
        form.addresult("dirpath", this.getdirpath(form));
        return module.findpage("list");
    }
在模板文件中有如下内容:
#foreach($info in ${dir.children})
目录名称:${info.title}
#end

关于业务逻辑层bean的配置:

<aop:config>
                <aop:pointcut id="cmsmanage"
            expression="execution(* com.easyjf.cms.service.*.*(..))" />
<aop:advisor advice-ref="cmsmanageadvice"
            pointcut-ref="cmsmanage" />
<tx:advice id="cmsmanageadvice"
        transaction-manager="transactionmanager">
        <tx:attributes>
            <tx:method name="get*" propagation="supports"
                read-only="true" />
            <tx:method name="query*" propagation="supports"
                read-only="true" />
            <tx:method name="*" propagation="required" />
        </tx:attributes>
    </tx:advice>
<bean id="cmsmanageservice"
        class="com.easyjf.cms.service.impl.cmsmanageserviceimpl">
        <property name="newsdirdao" ref="newsdirdao" />
    </bean>
在这里,当mvc层执行到$!info.getchildren()方法的时候,将会用到缓迟加载,由于spring的事务是配置在service层的,因此在执行service.querydirsbyconditions方法完成后就关闭了事务。因此运行程序就会出现类似下面的错误信息:


2007-03-28 00:39:35,750 error [org.hibernate.lazyinitializationexception] - failed to lazily initialize a collection of role: com.easyjf.cms.domain.newsdir.children, no session or session was closed
org.hibernate.lazyinitializationexception: failed to lazily initialize a collection of role: com.easyjf.cms.domain.newsdir.children, no session or session was closed
 at org.hibernate.collection.abstractpersistentcollection.throwlazyinitializationexception(abstractpersistentcollection.java:358)
 at org.hibernate.collection.abstractpersistentcollection.throwlazyinitializationexceptionifnotconnected(abstractpersistentcollection.java:350)
 at org.hibernate.collection.abstractpersistentcollection.readsize(abstractpersistentcollection.java:97)

  使用其它的mvc如struts、webwork乃至spring mvc都会有这样的问题,问题的核心是在事务启动及结束上,由于我们都习惯于在service层而非mvc配置及使用事务,导致了这样的问题。解决的办法其实很简单,就是把事务的启动放到mvc层,
    让mvc层的controller来开启事务,而让业务层的方法加入的事务中。比如,在easyjweb中,可以通过如下的配置来实现实现在action中开启事务:
  在spring配置文件中配置easyjweb的核心处理器,并把process方法添加到事务中,配置文件如下:

<aop:config>
        <aop:pointcut id="easyjwebprocessor"
            expression="execution(* com.easyjf.web.requestprocessor.process(..))" />
        <aop:advisor advice-ref="txeasyjwebprocessoradvice"
            pointcut-ref="easyjwebprocessor" />
    </aop:config>
    <tx:advice id="txeasyjwebprocessoradvice"
        transaction-manager="transactionmanager">
        <tx:attributes>
            <tx:method name="*" propagation="required" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <bean name="easyjweb-processor" class="com.easyjf.web.core.defaultrequestprocessor"/>
  只需要这样简单的配置,你会惊奇的发现,所有缓迟加载及其它由persitence context无效而引起的问题均解决了。

扫描关注微信公众号