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

Related Questions

Does anyone know how I can prevent the text in a table cell from
Does anyone know if it is possible to prevent a work item from being
Does anyone know how I can, in platform-independent C++ code prevent an object from
does anyone know how to prevent the failing mechanism of link_to_unless_current? f.e.: I have
Does anyone know if there is a way to prevent Sinatra from sending the
Does anyone know of a method to prevent the jQuery timepicker addon from automatically
Does anyone know of a way to prevent the browser from asking the user
Does anyone know how I can prevent my C# winform covering/going on top of
Does anyone know how to make this code stop the knob from rotating past
Does anyone know of a technique to prevent someone (me!) accidentally committing a file

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.