Results 1 to 2 of 2
  1. #1
    Monique7 is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default Solve LazyInitializationException in spring, hibernate ?

    I have a Spring app , many to many relationship between Order and Product with intermediary ProductOrder.

    If I save the ProductOrder directly to the db, it works fine, but I don't think that's the correct way, so I use the code below to add the connection to a Product by fetching and adding to a lazy collection.

    I had a LazyInitializationException , so I tried to solve it with Hibernate initialize, but now I'm getting org.hibernate.HibernateException: collection is not associated with any session. I debugged and session=null for the PersistentCollection is null . I thought the @Transactional annotation is meant to create begin a transaction and open a session, but it doesn't seem to work.
    I looked for some answers , and the cause is that the current session is not the same as when the object was created. I don't know how to try solving the problem, please help me!

    (I also have OpenEntityManagerInViewFilter in web.xml , but it doesn't seem to make any difference in solving this)

    Java Code:
    	@Transactional
    	public void addClient(...)
    	{
                    ...
    		Order order = new Order();
    		order.setClient(client);
    		order.setDate(new Date());
    		
    		order = orderService.save(order);
    		
    		for(Item item : items)
    		{
    			ProductOrder po = new ProductOrder();
    			po.setOrder(order);
    			po.setProduct(item.getProduct());
    			
    			Hibernate.initialize(item.getProduct().getProductOrders());
                  item.getProduct().getProductOrders().add(po);
    			productService.save(item.getProduct());
    
    }

  2. #2
    Monique7 is offline Member
    Join Date
    May 2014
    Posts
    7
    Rep Power
    0

    Default Re: Solve LazyInitializationException in spring, hibernate ?

    When I save the ProductOrder to order it works fine , but like this it throws the LazyInitializationExxception , please explain why , after all I thought it should have the same result?

Similar Threads

  1. Replies: 1
    Last Post: 02-05-2014, 12:34 PM
  2. Replies: 2
    Last Post: 05-10-2012, 10:45 AM
  3. Use Hibernate & Spring
    By onepseudoxy in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-13-2010, 10:46 AM
  4. Hibernate, JPA2, Spring???
    By lat3ncy in forum New To Java
    Replies: 1
    Last Post: 03-04-2010, 04:45 AM
  5. Replies: 0
    Last Post: 08-29-2009, 08:32 PM

Posting Permissions

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