Affichage des articles dont le libellé est java. Afficher tous les articles
Affichage des articles dont le libellé est java. Afficher tous les articles

lundi 5 mars 2012

How to solve the org.hibernate.LazyInitializationException

If you are building real world Spring-MVC /Hibernate applications you will probably have the honor to meet the org.hibernate.LazyInitializationException. I'm assuming that in the DAO's implementation you are using the HibernateTemplate and not the entityManager (and of course a sessionFactory instead of a entityManagerFactory).

The error you might have looks like this:

GRAVE: Servlet.service() for servlet [testDispatcher] in context with path [/secondtest] threw exception [Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.test.anotated.ArtData.colors, no session or session was closed] with root cause
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.test.anotated.ArtData.colors, no session or session was closed
 at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
 at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
 at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
 at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
 at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
 at com.test.controller.GetArtController.helloWorld(GetArtController.java:38)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

I have found a good explanation of the why on a webpage: 

"This error means that you’re trying to access a lazily-loaded property or collection, but the hibernate session is closed or not available . Lazy loading in Hibernate means that the object will not be populated (via a database query) until the property/collection is accessed in code. Hibernate accomplishes this by creating a dynamic proxy object that will hit the database only when you first use the object. In order for this to work, your object must be attached to an open Hibernate session throughout it’s lifecycle."

That is pretty nice! :)

The solution I've found in many web pages and forums is to force Hibernate  to perform an Eager fetching by using  fetch = FetchType.EAGER on the collection of the entity class. Once again, if you are building real world applications this kind of quickfix will not work.

For example, if your entity has one collection of objects (let's say a List<MyObject>), and each one of this objects has more than one collection of objects (let's say 2 or 3  List<String>); writing  fetch = FetchType.EAGER next to each collection  will probably  carry you to have the  javax.persistence.PersistenceException: org.hibernate.HibernateException: cannot simultaneously fetch multiple bags.  Now,  instead of  trying to fix our brand new bug, lets try to fix our configuration to avoid having the first LazyInitializationException in a clean way.

How to avoid the LazyInitializationException:

All you need is to mantain the session of your sessionFactory open while the processing the request. This is possible by adding an OpenSessionInViewFilter filter in your web.xml that will bind the session to the thread of the entire processing of the request.

Here is an example of an web.xml file, we are defining the filter on lines 17-20 and doing the mapping it to all the URLs of the application on lines 22-25.
<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>Spring Web MVC Application</display-name>
 
<!-- Listener to create the Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/spring-master.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 
  <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>
 
 <servlet>
  <servlet-name>testDispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>testDispatcher</servlet-name>
  <url-pattern>*.htm</url-pattern>
 </servlet-mapping>

</web-app>

By doing this, the filter will mantain the session opened (during the request handling) and allow the Lazy fetching.

Try this sample project:

I did this little sample spring-mvc / hibernate project (using eclipse)  if you want to see the complete configuration and test its execution.

You can download the project and all needed jars (14.3 M)
hibernate-sample.zip

or just the project with an empty \war\WEB-INF\lib directory (23 K)
hibernate-sample2.zip

To run this example you must have already a database installed and tomcat configured:


  1. Unzip the file with the project
  2. Import it to eclipse
  3. Edit the src/META-INF/spring/spring-datasource.xml to match your database (I have used Postgresql in this example)
  4. Run it whith Run on Server (if the option Run on Server don't appear read the next steps)
  5. enjoy :)

If Run on server option don't appear
  1. Rightclic on the project
  2. Choose properties
  3. Choose Project Facets
  4. Select "Dynamic Web Module" and Java and then clic on "Further Configuration" available.
  5. Set the "Content directory" as war and don't check the generate.xml option
  6. click OK
  7. Done!

Now if the project is running you can go to the URL http://localhost:8080/hibernate-sample/ and see this.




mercredi 11 janvier 2012

Changes on "Developing a Spring Framework MVC application step-by-step" using spring 3 and tomcat 7.x

I've seen a lot of questions over different forums of people following this spring tutorial.

This is a really nice tutorial if it's your first time using Spring MVC but  it was written for using Tomcat 5.x or 6.x and Spring 2.5

If you are using tomcat 7.x and Spring 3 you can still follow the tutorial but you need to do some small changes on the Basic Application and Environment Setup part:


  • In step 1.3 "Deploy the application to Tomcat"
    1. In the file springapp/build.xml, change org.apache.catalina.ant.InstallTask (now deprecated) by org.apache.catalina.ant.DeployTask and
    2. In the fileset where you include catalina-ant.jar include  include as well tomcat-coyote.jar and tomcat-util.jar like this:

      <fileset dir="${appserver.lib}">
               <include name="catalina-ant.jar"/>
               <include name="tomcat-coyote.jar"/>
               <include name="tomcat-util.jar"/>
      </fileset>
      
      
    3. In the file springapp/build.properties set your appserver home as
      appserver.home=${user.home}/apache-tomcat-7.0.23   
      and
    4. set the url of tomcat manager as
      tomcat.manager.url=http://localhost:8080/manager/text        
    5. In the tomcat-users.xml you need to use the roles provided in tomcat 7.x, you need at least the manager-script role, you can use the following xml:
      <tomcat-users>
          <role rolename="manager-script"/>
          <user username="tomcat" password="s3cret" roles="manager-script"/>
      </tomcat-users>
      

  • In step 1.7 "Copy libraries to 'WEB-INF/lib'",  just take into account that the  spring.jar is not longer provided by Spring 3. Instead of one single jar now spring has many of them, you can either choose to add just the ones you will use or just add all the jars to the  'war/WEB-INF/lib' directory. You will probably need to download the  commons-logging.jar from  here and add it to the lib directory as well. If you want, you can download this zip that contains the needed jars to follow the tutorial (therefore you don't need to download the jars described in the next two steps).
  • In step 1.9 "Write a test for the Controller" you  will probably need to download the  junit-4.10.jar from  here and add it to the lib directory.
  • In step 2.1. "Configure JSTL and add JSP header file" instead of  adding to your project the standard.jar and the  jstl.jar  download the jars that are here: http://jstl.java.net/download.html
  • In the step 4.5 "Adding a form" just take into account that both spring.tld and spring-form.tld can be found in the spring-webmvc-3.0.x.RELEASE.jar file.  To use them the JAR file must be in the classpath. Just add the following lines to the JSP files:
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    
By doing this little config changes you should be able to follow the rest of the tutorial without any problems.