Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to solve Spring/Hibernate java.lang.StackOverflowError ?

 
Monique Johnson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an app with a many-to-many relationship between entities (Product, Order) , the intermediary table is ProductOrder


the first 2-3 times I lauched the app it worked fine, but now I get

java.lang.StackOverflowError

The DB was updated on the first runs (table for ProductOrder was populated with the correct data).

Here is the code for Product, Order, ProductOrder (id and other fields excluded) :

Product code:


Order code:


ProductOrder code:


ProductOrderId code:



The stack trace changes a bit with every launch , but the main error is still java.lang.StackOverflowError , full stack trace is availale at http://tny.cz/f9f52aee
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which version of hibernate do you have? This is an old bug in hibernate. Happens when you set both sides of a bidirectional relationship as eager
 
Monique Johnson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:Which version of hibernate do you have? This is an old bug in hibernate. Happens when you set both sides of a bidirectional relationship as eager


I'm using Hibernate version 3.2.0.Final , I tried now to update to 4.2.8.Final but I still get the java.lang.StackOverflowError
Is the bug not resolved in this version?
How to solve my problem , please help me )

I tried setting the FetchTypes as Lazy (earlier) but was getting lazyInitialization exception and the solutions from the internet didn't help )
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you upgraded it correctly? The error that you see seems a lot like This bug, and they fixed it in 3.6. Try 3.6. Hopefully it hasn't popped up in 4.2 again!


If you make the relationship lazy, you will have to access the lazy property from inside the session. You won't be able to access the property after the session has been closed. Normally, there are 2 ways of solving this

A) in you DAO, convert your ORM bean to a value bean. This way you will be accessing properties of the ORM bean from inside DAO only. I don't like this method because it's so ugly. Essentially, you are making that property eager, but you are introducing a new dot class just to get around hibernate's bug
B) add another method in the DAO to get the lazy property. The method can take product id and return product orders. Inside the DAO, you can query hibernate for product again, and get product orders. Or you can directly run an HQL to get product orders by product id.


Really, you shouldn't be making a collection property eager loaded unless you are absolutely sure that it's going to be a small collection. It will add a lot of overhead if your product has like 2 million orders. To me, it makes more sense to have the DAO layer expose it as another method rather than adding it as a property on the object. That way orders are retrieved only when you need them.
 
Monique Johnson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think I upgraded correctly , since it works all right.

I tried to set FetchType to LAZY .
As I understand you suggested to use a query to get the orders so smth like this "select o from Order o left join o.productOrders po where po.pk.product = :product" ?
But I have to associate the new order with the product should I just insert directly in the OrderProduct table with a query ? I don't think that's correct and I'm new to all this so I'd appreciate any suggestions/explanations
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to say without looking at the what else you are doing in the code. If you are saving Products and their orders together, add the orders list to the product and save product. If you are just adding a new order to existing product, you can just create an instance of OrderProduct and save it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic