Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 34

Thread: LazyInitializationException Hibernate Spring Help!!!

  1. #11

    Default

    Quote Originally Posted by logixplayer View Post
    Perhaps what you are trying to say is that there should only be one overall transaction in the controller?
    sorry if I didn't answer correctly before, I want to say something like this:

    you have your controller:

    Code:
    class controller{
    public ModelAndView onSubmit(Object command){
    method A (propagation=Propagation.REQUIRED)
    method B (propagation=Propagation.REQUIRED)
    method C (parameter mehotd A, parameter method B)(propagation=Propagation.REQUIRED) 
    // here LazyInitializationException because transaction of each method are closed, because
    ModelAndView onSubmit(Object command) is not transactional
    }
    }
    one solution to this would be something like this:
    Code:
    class DAO{
    propagation=Propagation.REQUIRED
    method TOCONTROLLER{
    method A (propagation=Propagation.REQUIRED)
    method B (propagation=Propagation.REQUIRED)
    method C (parameter mehotd A, parameter method B)(propagation=Propagation.REQUIRED) 
    //here you will not have exception because parent's method is transactional and required, so this is a transaction over other transactions included in sub_methods
    }
    }
    IMHO.

    what dou you think?

  2. #12
    Join Date
    Jun 2009
    Posts
    106

    Default OpenEntityManagerInViewFilter LazyInitializationException HELP!!

    Category controller is calling my category service.
    Category Service is defined as such:

    Code:
    	@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
    	public List<Category> findByName(Object name, int[] rowStartIdxAndCount){
    		 List<Category> result = iCategoryDAO.findByName(name,rowStartIdxAndCount);
    		return result;
    	}
    So it is defined as
    1)@Transactional
    2)propagation=Propagation.REQUIRED
    3)readOnly=true

    Now, after I get back the List of Categories, I do this:

    Code:
    Set<Category> subCategories = category.getCategories();
    and this is when I get the lazyLoadException.
    Once again, my getCategories method is declared as LAZY:
    Code:
    	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "category")
    	public Set<Category> getCategories() {
    		return this.categories;
    	}
    Essentially this exception is happening outside of the first transaction (to get categories). I thought that by using OpenEntityManagerInViewFilter it can somehow re-open/re-attach that original session ....

    web.xml:
    Code:
    	<filter>
    	    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    	    <filter-class>
    	        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
    	    </filter-class>
    	      <init-param>
    			<param-name>singleSession</param-name>
    			<param-value>false</param-value>
    		</init-param>
    	    
    	</filter>
    	
    	<filter-mapping>
    	    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    	    <url-pattern>/*</url-pattern>
    	</filter-mapping>
    Any thoughts?

  3. #13

    Default

    here is your problematic code:
    Code:
    		List<Category> categories = categoryService.findByName("Autos", null);//here session is opened
    		//	should only be one category in actuality
    		for (Category category : categories) {
    			System.out.println("category: "+category.getName());//here session is closed
    			
    Set<Category> subCategories = category.getCategories(); LAZY EXCEPTION HERE because youe session is closed
    to solve this you must decouple your dao code from controller, because as I said before your method is not transactional

    Code:
    public ModelAndView onSubmit(Object command)
    so your option is to do a dao method doing all this;
    Code:
    @Transactional
    class DAO{
    
    @Transactional(propagation=Propagation.REQUIRED, readOnly=true)
    public Set returnSet(){
    
    List<Category> categories = categoryService.findByName("Autos", null);
    		
    	for (Category category : categories) {
    			
    Set<Category> subCategories = category.getCategories();
    return subCategories ;
    }
    }
    with this method you will be sure your returnSet() transaction will overwrite inner transactions, and then in your Controller class you can call this method without exception.

    finally your controller class would be like:

    Code:
    public class CategoryController extends SimpleFormController {
    
    	public ModelAndView onSubmit(Object command) throws ServletException {
    		CategoryBean categoryBean = (CategoryBean) command;
    		System.out.println("the name from the command bean is: "+categoryBean.getName());
    		
    		ICategoryService categoryService = (ICategoryService)DataAccessServiceFactory.getBean("iCategoryService");
    		ITopicService topicService = (ITopicService)DataAccessServiceFactory.getBean("iTopicService");
    			
    Set<Category> subCategories = returnSet(); //CODE CHANGED
    			for (Category subCategory : subCategories) {
    				System.out.println("subCategory: "+subCategory.getName());
    				//Set<Topic> topics = subCategory.getTopics();
    				List<Topic> topics = topicService.findByCategoryID(subCategory.getCategoryId(), null);
    				System.out.println("****now to get topics if there are any!****");
    				for (Topic topic : topics) {
    					System.out.println("topic: "+topic.getTitle());
    					System.out.println("****now to get submissions if there are any!****");
    					Set<Submission> submissions = topic.getSubmissions();
    					for (Submission submission : submissions) {
    						System.out.println("submission topic: " +submission.getTopic());
    						System.out.println("submission title: " +submission.getTitle());
    					}
    				}
    			}
    what do you think?

  4. #14

    Default

    now I see other think, maybe you will have same problem with other code like this, because you are doing the same.
    Code:
    List<Topic> topics = topicService.findByCategoryID(subCategory.getCategoryId(), null);
    				System.out.println("****now to get topics if there are any!****");
    				for (Topic topic : topics) {
    					System.out.println("topic: "+topic.getTitle());
    					System.out.println("****now to get submissions if there are any!****");
    					Set<Submission> submissions = topic.getSubmissions();

  5. #15
    Join Date
    Jun 2009
    Posts
    106

    Default JPA Hibernate Spring Help!

    Appreciate the reply but that's really not the way I want to go.
    The solution you posted is really not very flexible and it totally defeats the purpose of LazyLoading.

    I would like to lazy load when I need to, which is why there is a OpenEntityManagerInViewFilter in the first place.


    Note the following definition of OpenEntityManagerInViewFilter:

    Code:
    Servlet 2.3 Filter that binds a JPA EntityManager to the thread for the entire processing of the request. Intended for the "Open EntityManager in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.
    The definition is perfect. It allows lazy loading in web views despite the original transactions already being completed.
    But it's still not working. Some configuration missing somewhere!!!

    Thoughts and solutions? Thanks!

  6. #16

    Default

    ok, good luck.

  7. #17

    Default

    I have seen one thing, in docs I have seen this:

    http://static.springsource.org/sprin...iewFilter.html

    Code:
    Servlet 2.3 Filter that binds a Hibernate Session to the thread .....

    what version of servlet are you using?

    and I have seen some spanish and portuguese posts talking about this, and they had similiar problem , solving it changing order of OpenEntityManagerInViewFilter inside web.xml putting it first, and then other delcarations.
    Last edited by duardito; Sep 1st, 2009 at 01:13 PM.

  8. #18
    Join Date
    Jun 2009
    Posts
    106

    Default JPA Hibernate Spring Help!

    Quote Originally Posted by duardito View Post
    I have seen one thing, in docs I have seen this:

    http://static.springsource.org/sprin...iewFilter.html

    Code:
    Servlet 2.3 Filter that binds a Hibernate Session to the thread .....

    what version of servlet are you using?

    and I have seen some spanish and portuguese posts talking about this, and they had similiar problem , solving it changing order of OpenEntityManagerInViewFilter inside web.xml putting it first, and then other delcarations.
    Yes I also indicated this in my previous posts,
    I noticed it said servlet 2.3 as well which made me go hmmmm.
    I am using Servlet 2.5 and JSP 2.1.
    Is this a problem?
    I also took the advice of others and have made the OpenEntityManagerInViewFilter the very first thing in my web.xml, observe:
    Code:
    <?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">
      
         <!-- Include this if you are using Hibernate -->
    	<filter>
    	    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    	    <filter-class>
    	        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
    	    </filter-class>
    	      <init-param>
    			<param-name>singleSession</param-name>
    			<param-value>false</param-value>
    		</init-param>
    	    
    	</filter>
    	
    	<filter-mapping>
    	    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    	    <url-pattern>/*</url-pattern>
    	</filter-mapping>
    	
      	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    					<!-- classpath:applicationContext.xml  -->
    					<!-- classpath:/data-access-context.xml  -->
    					classpath:data-access-context.xml
    		</param-value>
    	</context-param>
      
    	<welcome-file-list>
    	  <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
    	</welcome-file-list>
    
        <!-- Spring MVC Configuration -->
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.person</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.tribute</url-pattern>
        </servlet-mapping>
        
    	<jsp-config>
    	    <taglib>
    	      <taglib-uri>/spring</taglib-uri>
    	      <taglib-location>/WEB-INF/spring-form.tld</taglib-location>
    	    </taglib>
    	</jsp-config>
    </web-app>
    Any thoughts or ideas? Thanks a lot.

  9. #19

  10. #20
    Join Date
    Jun 2009
    Posts
    106

    Default OpenEntityManagerInViewFilter

    Quote Originally Posted by duardito View Post
    Hi, I from that post, I tried this:

    Code:
    	<filter-mapping>
    	    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    	    
                <servlet-name>dispatcher</servlet-name> 
    	</filter-mapping>
    instead of using <url-pattern>/*</url-pattern> because dispatcher is my spring servlet.
    Code:
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
    but it didn't work. So I reverted back...
    still stuck...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •