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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:12:00+00:00 2026-05-30T15:12:00+00:00

I have a problem with hibernate where it is doing N+1 SELECTS when using

  • 0

I have a problem with hibernate where it is doing N+1 SELECTS when using a DTO object.

For example, this JPQL query:

SELECT com.acme.MyDto(t) FROM Thing t

Where the constructor of MyDto is something like:

public MyDto(Thing t) {
   ...
}

Which results in a query of something like:

SELECT t.id FROM thing t WHERE condition

followed by all the single queries for each row, ie:

SELECT t.id, t.column1, t.column2 FROM thing t WHERE t.id = 1
SELECT t.id, t.column1, t.column2 FROM thing t WHERE t.id = 2
SELECT t.id, t.column1, t.column2 FROM thing t WHERE t.id = 3
...

However if the constructor doesn’t accept the Entity, but instead each individual column, then hibernate behaves as you would expect, ie:

public MyDto(Integer id, String column1, String column2) {
   ...
}

Then the generated SQL looks like this:

SELECT t.id, t.column1, t.column2 FROM thing t WHERE condition

Aside from creating DTO constructors that takes every column, is there a way to coax hibernate to just select all the columns at once from the beginning?

The table that we’re working with has 100+ columns spread across embeddables, so it’s pretty annoying to maintain a huge constructor. The table is hugely normalised and has no joins.

  • 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-30T15:12:01+00:00Added an answer on May 30, 2026 at 3:12 pm

    Read your question wrong the first time… I don’t recall using DTOs if they just take the whole entity and not just some specific columns, so I’m not sure why Hibernate behaves like that when you use a whole entity as a parameter in DTO constructor. Anyway, you could work around it by just gettin the actual Things via a query and then construct the DTOs in a loop, something along the lines of:

    public List<ThingDTO> getThingDTOs( ... )
    {
        Query query = em().createQuery("FROM Thing t WHERE ...");
        query.setParameter( ... );
    
        List<Thing> things = query.getResultList();
    
        List<ThingDTO> thingDTOs = new ArrayList(things.size());
        for(Thing t : things)
        {
            thingDTOs.add(new ThingDTO(t));
        }
    
        return thingDTOs
    }
    

    It ain’t pretty, but this way Hibernate should fetch all the needed rows in one go

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

Sidebar

Related Questions

I have a problem with a query using hibernate. The entity class EntityClass has
I'm quite new to hibernate and have stumbled on this problem, which I can't
I have a problem using Linq to NHibernate to load an object and eagerly
I have a nHibernate query like this ICriteria query = session.CreateCriteria(typeof(MyResult)) .Add(Expression.Eq(ResultTypeId, myResult.ResultTypeId)) Problem
This is a real beginner Hibernate problem. I have a problem with the mappings
Hey I have a small problem doing a query: the query line is: Query
I'm running into a performance problem in my SQL using Hibernate's DetachedCriteria. I have
I have an odd problem where Hibernate is running more queries than I've asked
I have a jar on the classpath which contains a hibernate.properties. This causes problems
I have a class that is mapped to a table using NHibernate. The problem

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.