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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:07:19+00:00 2026-05-11T22:07:19+00:00

does anyone know how to prevent hibernate from loading a collection or many-to-one assocation?

  • 0

does anyone know how to prevent hibernate from loading a collection or many-to-one assocation?

i have a use case where i need to load an object, but the calling program can determine whether certain properties are loaded or not. the same goes for part of a collections: certain rows have to be fetched, others not.

domain classes:

public class SomeClass {
    private SomeClass parent;
    private Set<OtherClass> collection;
}

public class OtherClass {
    private Date startDate;
    private Date endDate;
}

public class Dao {
    public SomeClass loadClass(long id, boolean parents, boolean historicalData) {
         // load SomeClass
         // if parents == true, load parent, otherwise leave <null>
         // if historicalData == false, load OtherClass where endDate == <null>
    }
}

I have thought of 2 solutions, but I ont to now if its possible with an criteria or query.

Solution 1 is don’t make the assocation from SomeClass to OtherClass and the parent/child relation in the hibernate configuration and load the assocation in the code.
Solution 2 is to define different mappings with different entity-names with serve the special cases.

In this case the caller can be in an other JVM or transaction, so the session is closed, thus lazy loading is not an real option.

  • 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-05-11T22:07:19+00:00Added an answer on May 11, 2026 at 10:07 pm

    Write an Criteria query and use the setFetchMode method to change the fetching behavior for the parent and createCriteria method to additionally query the collection.

    Criteria c = session.createCriteria(SomeClass.class).add(Restrictions.idEq(id));
    
    if (parents) {
      c.setFetchMode("parents", FetchMode.EAGER);
    }
    if (historicalData) {
      c.createCriteria("collection", Criteria.LEFT_JOIN)
       .add(Restriction.lt("date", date))
       .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    }
    return c.uniqueResult();
    

    …this is assuming the default setting is LAZY (as per Hibernate default).

    It is no option in Hibernate to leave properties null that do have a value (in this case a many-to-one association. The field will hold a proxy that can be used to lazily load the referred object. If the code never touches the object, it will not get loaded.

    The same is true for the collection: if not fetched, it will hold a collection-proxy (a hibernate class that implements a collection interface), which will only load the contents when invoked.

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

Sidebar

Ask A Question

Stats

  • Questions 150k
  • Answers 150k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes. I recommend you take a look at Chapter 8… May 12, 2026 at 9:45 am
  • Editorial Team
    Editorial Team added an answer All (decent implementations of;-) functional languages optimize tail recursion, but… May 12, 2026 at 9:45 am
  • Editorial Team
    Editorial Team added an answer For future coding, you should make sure you follow good… May 12, 2026 at 9:45 am

Related Questions

does anyone know how to prevent the failing mechanism of link_to_unless_current? f.e.: I have
When I use the PrintOut method to print a Worksheet object to a printer,
In my webpage, I want the website to greet the user, but the username
I would like to create a table to store device settings. The table has

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.