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 8219483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:10:45+00:00 2026-06-07T13:10:45+00:00

i am writing one function in hibernate to recursively initialize all properties of object

  • 0

i am writing one function in hibernate to recursively initialize all properties of object recursively so whole object graph is loaded.

i have two complex scenarios where i need to use this

1) self composite object like category and sub category …

@Entity
public class Category {

    @Column(nullable = false, length = 50)
    @NotNull
    @Size(max = 50, message = "{50}")
    protected String name;

    @ManyToOne(targetEntity = Category.class, fetch = FetchType.LAZY, optional = true)
    private Category parentCategory;
    }

2) complex object graph which has lot of object to initialize before it can be used.

problem is i can not use eager fetching because i need this whole object graph only in selective cases, and i want to have generalize code so don’t need to write HQL queries for objects.

i have written partial some code for this,

public void recursiveInitliaze(Object obj) throws Exception {
    if(!Hibernate.isInitialized(obj))
        Hibernate.initialize(obj);
    PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(obj);
    for (PropertyDescriptor propertyDescriptor : properties) {
        Object origProp = PropertyUtils.getProperty(obj, propertyDescriptor.getName());
        if (origProp != null) {
            this.recursiveInitliaze(origProp);
        }
        if (origProp instanceof Collection && origProp != null) {               
            for (Object item : (Collection) origProp) {
                this.recursiveInitliaze(item);
            }
        }
    }
}

but it has one issue, it ends into stackoverflow for method invocation because of bi-directional relationships. so how to detect there is bidirectional relationship or is there any better way to implement this?

i think fetch profile can also help but still want to try to implement this if possible as configuring fetch profile at current stage of project is difficult.

  • 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-07T13:10:47+00:00Added an answer on June 7, 2026 at 1:10 pm

    you can use Hibernate.initialize() on the object to initialise it. I’m not sure if this cascades. Otherwise you could check instanceof HibernateProxy which gives you an isInitialised property.

    Update: since initialize does not cascade you need to traverse the tree like you do already. Use a Hashset to keep track of objects you already processed.

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

Sidebar

Related Questions

I am writing this code that one function calls itself recursively. But I am
I am writing one function on Javascript which needs to address all the anynymous
I am writing Classic ASP program.In one function, I have to use 2 update
I am using this article of architecture http://blog.extjs.eu/know-how/writing-a-big-application-in-ext/ I have this one function where
i have a (perhaps stupid) question: im using 2 threads, one is writing floats
I am writing my own blog (every one should, it's good experience). I have
I'm converting my fields class read functions into one template function. I have field
I'm writing a PHP web service and one function. I want to set up
I am writing a module that provides one function and needs an initialization step,
I am writing a C project. Now I just doubt one function in the

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.