• SSH之懒加载异常解决方案


    1、设置成即时加载

    2、查询语句使用join fetch

    from Coupon coupon  left join fetch coupon.users  where coupon.id=:id  

    coupon的List<users> users属性是懒加载,但是查询结果需要users的内容,则使用 left join fetch

    3、使用OpenSessionInViewFilter过滤器

    web.xml

        <!-- 以下是Spring的OpenSessionInView实现 ,唯一要求:放在struts2的过滤器前面-->
        <filter>
            <filter-name>openSessionInViewFilter</filter-name>
            <filter-class>
                org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
            </filter-class>
            <!-- 如果是singleSession=false的话, 就不会在每次的整个request的过程中使用同一个hibernate session, 
                而是每个数据访问都会产生各自的seesion,等于没有OpenSessionInView. -->
            <init-param>
                <param-name>singleSession</param-name>
                <param-value>true</param-value>
            </init-param>
            <!-- 指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory。 
                如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外。所以默认可以不写 -->
            <init-param>
                <param-name>sessionFactoryBean</param-name>
                <param-value>sessionFactory</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>openSessionInViewFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!-- openSessionInViewFilter在struts2前面 -->
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>

    Done!

  • 相关阅读:
    【免费】承接各类SharePoint项目开发工作
    数据仓库(一)Introduction
    几种极其隐蔽的XSS注入的防护
    Google SEO优化技术的12个要点总结
    数据结构算法必备
    算法数据结构
    python发邮件
    MySQL数据库中的Date,DateTime,TimeStamp和Time类型
    使用.htaccess实现301重定向
    SharePoint 2010 初次安装时使用向导启动的服务
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3913210.html
Copyright © 2020-2023  润新知