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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:14:21+00:00 2026-06-07T18:14:21+00:00

We are using Hibernate 4.1.4 with an entity manager implementation and are very pleased

  • 0

We are using Hibernate 4.1.4 with an entity manager implementation and are very pleased with it. When loading a single entity or small sets of entities (10-50 + sub-entities) we get excellent performance and all our joins work great and are bi-directional. Editing/saving/deleting is a breeze!

Loading 2500 Entities & Related Data for Reporting

We are running into problems when we try to load hundreds or thousands of entities along with sub-entities. In one example we have about 2500 base entities along with two one-to-one and three one-to-many joins. This results in 2500 x 5 +1 queries. Takes about 30 seconds to execute with our database (DB2).

We use a dynamic query and the entity manager create query method:

http://docs.jboss.org/hibernate/orm/4.1/javadocs/

http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html

Attempting to coax Hibernate to use Joins

Our observations have been that no matter what we try, we can’t seem to get hibernate to execute loads of sub-entities as joins on the primary query. When loading a single entity using the find method and by primary key hibernate uses joins, but when multiple base entities are loaded it does not.

We have tried criteria api with the same result.

Some Success Retrieving Multiple Entities in Query

Example:

select a,b from table1 a left join table2 b

We have had partial success doing the joins manually in our query and retrieving the individual entities via tuples but this bypasses our annotated entity model and is not the preferred method (if we can avoid it).

http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#objectstate-querying-executing

The problem we have encountered with tuples is they are detached from the primary entity (which seems logical as they are not loaded via annotated relationships) and there seems to be no way to re-attach them. After manually adding all the one-to-many records in the list of an entity, when hibernate hits the get method for the list, it still tries to eager/lazy load the list (despite the list already being set).

Attempted Optimization – Preloading of Data Sets

Another optimization technique we attempted was to pre-load sets of data in advance. After loading all the core entities we built a list of the pk’s and fired a single query off to load all the one-to-many sets of data. But again when hibernate hits the getter for the list data it again tries to eager/lazy load the list ignoring what is already pre-loaded in the session (need to enable caching perhaps?).

Suggestions?

We are going in a number of different directions with little success and are hoping someone might be able to point us down the right path (or point out a new one!)!

  • 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-07T18:14:22+00:00Added an answer on June 7, 2026 at 6:14 pm

    Seems I have figured it out myself! Two strategies fixed the issue:

    1. Join fetch in the query takes care of a one-to-many join! Of course you can only do one join fetch as Hibernate will throw the cannot simultaneously fetch multiple bags exception.

      Example:

      LEFT OUTER JOIN FETCH t.table3 t3
      
    2. For additional joins @Fetch(FetchMode.SUBSELECT) did the trick!

    Effectively reduced the total queries from thousands to just a few. In one example, 188 core entities were selected and a total of 12 queries were executed. The extra queries are mostly one-to-one look-ups and have no significant effect on the total execution time (on average 1-2 seconds). Whether 188 or 1888 records are selected, the total number of queries does not increase significantly.

    If anyone would like more information, feel free to contact me or leave a comment! 🙂

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

Sidebar

Related Questions

If I save an entity using a Hibernate session, and then probe to get
I have a problem with a query using hibernate. The entity class EntityClass has
How can I update an entity effectively in hibernate just as by using SQL.
I'm having some problems using cascade operations on hibernate. I have one entity (TripleDBmodel),
I'm using JPA 2 with Hibernate 3.6.8 as the implementation. Let's say we have
I'm using Hibernate's EntityManager as a JPA implementation. What I want is logging of
My web application is using Java, Hibernate's JPA implementation (EntityManager) and Spring. What are
I am using Hibernate to map objects to entities and I have started to
The JPA 2.0 Provider Hibernate is throwing exception while preparing configuration for entity manager
I'm using Hibernate 3.5.6 as my JPA 2.0 implementation. I'm attempting to build an

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.