Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:15:47+00:00 2026-06-17T05:15:47+00:00

I have the following java JPA objects which are converted to json in my

  • 0

I have the following java JPA objects which are converted to json in my spring rest controller method, they are

class User{
    @JsonManagedReference
    @OneToMany
    private Portfolio portfolio;
    .....
}

class Portfolio {
     @JsonBackReference
     @ManyToOne
 private User user;

    @JsonManagedReference
     private List<Order> orders;
     .....
}

class Order {
     @JsonBackReference
 private Portfolio portfolio;

     private User user;
}

In my application I need 2 services:

  1. Logon:
    returns the User object, with the Portfolio and List of Orders object, I don’t need the Order.user object for this.

  2. GetOrders:
    returns a List of Orders and I need the Order.user object with this.

Order.user is not the same as the parent User object.

My question is how to do I avoid an infinite recursion problem with the User object? Or is this just a bad design?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:15:49+00:00Added an answer on June 17, 2026 at 5:15 am

    The problem with @JsonManagedReference and @JsonBackReference is that they handle a very specific kind of (direct) cyclic reference well, but are not designed to process anything outside of that narrow window (where a single parent object is directly related to one or more child objects). Unfortunately, your use case doesn’t fall within that window.

    So you have two options:

    • Manual workaround – In your service class you would need to remove (make null) the order.user property, prior to serializing the top level user object. And conversely, you would need to remove (make null) the user.portfolio.orders property, prior to serializing the orders.

    • Framework workaround – Upgrade to Jackson 2.0+ and use @JsonIdentityInfo. It can properly handle object references in graphs, many-to-many relationships, deep object trees, and more. Some documentation and examples.

    You can also get around the problem if you have control over the ObjectMapper. If you do then you simply need to specify a custom serializer to use depending on which of the objects you are returning, but since you are working within the Spring framework I do not believe you have direct control over the mapper.

    My personal recommendation would be to upgrade your Jackson library and use @JsonIdentityInfo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following java class: package domain; //imports @Entity public class User {
Say suppose I have the following Java code. public class Example { public static
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
Say I have the following simple java bean: class MyBean { private Date startDate;
I have following UML class diagram and java coding for each, please tell me
Using: Glassfish 3.1.2, EclipseLink. I have the following three-classes JPA model: @Entity public class
I have a web application written in Java (Spring, Hibernate/JPA, Struts2) where users can
I have the following JPA (2.0.2) entities: Employee @Entity @Table(name = T_EMPLOYEE) public class
i have the following problem. I use Spring and JPA (Hibernate) to persist Data
I have the following existing DB schema, which I'd like to recreate with Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.