The project encountered in the ssh lazy loading problem (finally solved !!!)

hibernate3.3.2 + spring3.0.3 + struts2.2.1

Lazy loading (Load On Demand) is a unique and powerful data access method, which can automatically when the user scrolls the page to get more data, and the new data obtained will not affect the display of the original data, while minimize the consumption of server resources. (Baidu said)

Popular point is not looking to identify an object associated with the object of his, but need the associated objects (or properties) only to find the database, also known as lazy loading.

In general, we are in the many-to-one of many set lazy = false, which is available to our own hibernate.

Then lock up the spring session, we can put it another way: OpenSessionInViewFilter

OpenSessionInViewFilter is a Spring provided a support class for Hibernate

Generally, we can be so configured directly in the web.xml

<!-- Spring Provides avoidance of Hibernate lazy loading exception filters
   Let the Session after the completion of the request for interpretation and close  , Thereby avoiding lazy load exception   -->
  <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>

<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

This will save a lot of trouble to many places do not need to set up in each of the lazy = false

This method should be very popular, I also got from the Internet. Yet the novice I was directly used to take over the project are not deployed from the effects of N times, really depressing. Each error: org.hibernate.LazyInitializationException: could not initialize proxy - no Session.

Obviously, session closed. Tragedy

Collapse at the time, why the Internet are saying, but it is always wrong.

Know that today I saw this article http://www.javaeye.com/topic/32001

Suddenly the fog suddenly.

Since the spring tube from the sessionFactory, session must also be obtained through his fishes Yeah, so this part of the mapping of the mapping should also be placed on the back of struts2, after this class, and then do the real Action codes, and finally Hibernate Session according to the situation will be closed.

The following is the complete web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

        <!-- struts2 -->
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>
                org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
</filter>

<!-- spring  Consolidation  struts2 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:daoContext.xml,classpath:actionContext.xml,classpath:serviceContext.xml,classpath:applicationContext.xml</param-value>
</context-param>

<!-- Spring Provides avoidance of Hibernate lazy loading exception filters
   Let the Session after the completion of the request for interpretation and close  , Thereby avoiding lazy load exception   -->
  <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>

  <!--  The following 2  mapping Can you swap position   -->

  <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>  

  <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

ok, so lazy save in each of the many configurations.

Evidently not enough for understanding the underlying ah. To fuel it!

分类:Java 时间:2010-11-11 人气:129
分享到:
blog comments powered by Disqus

相关文章

  • Filter learning filter 2010-09-21

    ///////// Development of web applications, you can develop multiple filter, These filter can be achieved by one or a group of resources to access WEB intercept In order to achieve some special features. For example, the competence to achieve URL leve

  • Oracle select --------- having conditions where condition filter and filter 2010-06-28

    --------- Having conditions where condition filter and filter rows using the HAVING clause to select the GROUP BY clause HAVING clause to the set of conditions and the SELECT statement with WHERE clause similar to the way interaction. WHERE clause se

  • Filter Driver: filter (modification) to accept packets 2010-04-20

    Filter drivers can implement a simple firewall. It can filter all the receivers to the package, and then analyze the deal, Finally, decide if Wangshang passed to the Protocol Ceng, and finally to Ying Yong Cheng. Our principle is not received NBL mod

  • The Beauty of Mathematics Series 21 - Bloom filter (Bloom Filter) 2010-02-09

    In daily life, including in the design of computer software, we often have to determine whether an element in a collection. For example, in word processing software, you need to check whether an English word spelled correctly (that is, to determine w

  • Bloom filter (Bloom Filter) 2011-01-07

    In daily life, including in the design of computer software, we often have to determine whether an element in a collection. For example, in a word processor, to check whether an English word spelled correctly (that is, to determine whether it is in a

  • Mathematical beauty of Series 21 - Bloom filter (Bloom Filter) 2011-06-22

    Today in the training of a product currently used in hearing Bloom Filter, is unfortunately not before a close look, now under re-study, to incorporate ideas. In daily life, including in the design of computer software, we often have to determine whe

  • support for png transparency ie6 image file loading problem filter 2010-05-14

    ie6 png transparency support needed filters, but filters in the loading picture, picture of the relative position is not outside the chain relative to the location of css file, but the location relative to the html file, remember.

  • tomcat under linux to access this function, the external solution can not access the http://localhost:8080/ 2010-08-23

    In general, the reason is the firewall, open port 8080 on the line, you can use the following command: # Service iptables stop <ENTER> Of course this is only a temporary solution, the server is started on the failure. Can use the following methods:

  • EXT data loading problem 2010-11-07

    Flex3 recently learned a lot, thinking begin to review before the learned ext, start to do one or two projects. I EXT stays on a lot, on the basic content is still very clear, also has a variety of advanced techniques applied to the project hopes to

  • Solve the data too long for column''at row 1 of the method 2011-10-21

    Chinese data to the database insert occurs when the data too long for column 'columnname' at row 1 error causes the problem: data character encoding problem-solving method: (1) modify the mysql installation root directory my.ini (MySQL Server Instanc

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.392 (s). 12 q(s)