• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate and TomEE+ bidirectional association causes json recursiveness

 
Greenhorn
Posts: 19
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using TomEE 9.0.12 with Hibernate 5.3.7 and I am currently getting a stackoverflow exception due to json recursiveness. Note that I am not using Jackson, and ideally don't want to, but the standard that comes with TomEE+: org.apache.johnzon.

The exception:



Here is my User.java class:



And here is my Post.java



The issue

The issue is that they will keep referring back to each other. User will refer to posts, which will refer back to the user, which will again refer to posts-- and so the evil circle continues.  I know that I can use org.apache.johnzon's annotation @JsonbTransient on "user" in User.java to make it not serialize it, but that makes so that it won't ever show (which I want, but conditionally).

Why do I want it like this? Because, let's say GET /users/1 is accessed, 1 being the id of the user, I want to show all the user's posts (or at least the ID of them), but don't want it to refer back to the user (nested user inside post inside user). However, if GET /api/posts/3, 3 being the id, I want it to show the user which made the post, but not the posts again inside the user. So basically, the annotations must be conditional in a way, and specific.

Currently, this is how I am retrieving and showing the user (framework automatically converts returned object to JSON when the method is accessed).


Thank you in advance! I have searched for 5 hours but no solutions or reading material have solved my problem.
 
Marius Richardsen
Greenhorn
Posts: 19
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed this by just moving away from TomEE+ and to Spring Framework (with an embedded Tomcat Server), which uses Jackson as default instead of "org.apache.johnzon. With this, I was able to use the useful annotations:


This did so the JSON would look like this:


And so alike for User inside Post too, granted they're also marked with those annotations. This is exactly what I wanted and what the front-end web framework I use, EmberJS, and many alike, expect the data to be returned. So this is perfect for me. Also, after playing around with Spring for a little while with an embedded Tomcat server, I like it more too (I've been through Glassfish, TomEE+, and now Spring Framework with embedded Tomcat, and only now is it working as I wanted it to).
 
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic