• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

How to avoid overflow error with recursive entities [Hibernate 5, MySQL 8]

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
my problem consist in how to fetch a recursive table in a entity's field with out getting an "Exception occurred in target VM: java.lang.StackOverflowError"

I have an Activity class that realize a recursive table:


and a Permission class that has a many to many relations:


I'm using a JpaRepository abstraction to load database's data, when i try to access to activities filed it goes to OverFlow error.
It is another way to directly set a named query when it comes to retrieve tha activities field?
 
Saloon Keeper
Posts: 21248
137
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the first thing I'd try is to make those items be Lazy Fetch.

The default for JPA is Eager Fetching, and that can potentially load your entire database into memory, which isn't a good thing if the database is large.

It may kill your overflow problem, although actually, a loop in relationships is something that I'd normally expect Hibernate to detect and handle. Meaning that it's possible there's a bug in the version of Hibernate that you are using.
 
Luigi Mattino'
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,
in fact I think  I should use a FetchType.LAZY. I need to use a native query to reach the graph contained in the loop relationship: It is a way to inject this kind of query in the fetch mechanism?

Thanks.
 
Tim Holloway
Saloon Keeper
Posts: 21248
137
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can create both JPA JPQL and native SQL queries. Although I don't think you need to do that in your case. Just follow the graph via object references.

Here's a hint that can make assembling a limited in-memory graph easier. JPA mandates that the equals() and hashCode() methods for each Entity be distinct for that Entity instance (table row). So when building a graph with circular references, you can log their hashCodes. If you hit a hashCode value that's in your log, do an equals() comparison with your stored Entities to see if the record was entered previously. If so, you know you've closed the circle and can stop. That's basically how JPA caching works, except that in their case, they're just trying to see if a physical database read is required or if a requested record is already in cache and can be returned to the requester without the overhead of talking to the database.
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!