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

  • SEARCH
  • Home
  • 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 8165055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:27:03+00:00 2026-06-06T19:27:03+00:00

I am using Hibernate 4.1 and Spring 3.1. Consider the following User class having

  • 0

I am using Hibernate 4.1 and Spring 3.1.

Consider the following User class having references to other object including self. All the hibernate mappings I have defined are LAZY so will load the reference objects only when I use them in the jsp or code else where.

class User {
  private User createdBy;
  private Department department;
  private Project project;
  .....
  .....
}

The problem is when I want to return a JSON representation where it uses reflection to do it and there by does a deep serialization. Below is my controller code.

@ResponseBody
public User getUser(int id) {
  User user = [fetch user from service];
  return user;
}

Since I have self reference also it goes into infinite loop.

What is the solution to get away with this problem? I know that I need to use DTO pattern where I return a UserDTO instead but how many such methods I create. For example, at one place I need just the basic User attributes, at other place I need User and its Department, at other place I need User with Department and Project.

How many such methods I would need to expose. Is there any other way to resolve this?

  • 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-06T19:27:04+00:00Added an answer on June 6, 2026 at 7:27 pm

    The problem is that you have a circular reference and your JSON encoder is not handling it. So, you need to handle it manually.

    Creating a DTO as you commented would help. You can implement a Helper with one method for each level of decoding you need.

    You could use BeanUtils to copy each property from one bean to the other.

    Another solution (a bit more conceptual) would be:

    Implement a kind of decorator for each boundary method and apply dozer over the result. An example you could take here

    The Decoration occurs more or less like this:

    public interface Controller {
       User getUser(int id);
    }
    
    public class RealController {
       public User getUser(int id) {
          return dao.getUser(id);
       }
    }
    
    public class DecoratedController {
    
       private Controller realController;
    
       public User getUser(int id) {
          // here you will apply dozer or do it manually creating an object with so deep as you wish
          return flatUserStructure(realController.getUser(id));
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using hibernate, how can I persist a class with a List<String> field? Consider the
I'm trying to combine Spring with Hibernate using Annotations and I'm getting the following
I'm using Hibernate and Spring with the DAO pattern (all Hibernate dependencies in a
I am using Hibernate with spring. I have a model-class like this. @Entity @Table(name
I am using spring/hibernate. i have configured 5 datasources in applicationContext.xml file. All datasources
when using hibernate with spring, can someone explain how the session unit of work
I creating a web application using JSF,Hibernate,Spring. I have added a filter for checking
I am using spring/hibernate stand alone application. if i dont configure Transactions i am
I am using hibernate, spring, struts framework for my application. In my application, each
I'm using Hibernate Validator 4.0.2, Spring 3.0 and Hibernate 3.3.2 (which, as I understand

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.