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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:09:10+00:00 2026-06-04T10:09:10+00:00

I’m facing a problem, I have a query in JPA. as I have some

  • 0

I’m facing a problem, I have a query in JPA. as I have some collections I need to use left join fetch or inner join fetch

My problem is in using the setFirstResult and setMaxResult in order to bring back a precise number of result. every time i see the whole result is bring back AND only AFTER the maxResult is used.

Is there any way to make the maxResult before ?

Thanks a lot !

here it is more information :

my problem is when i use that :

startIndex = 0;
maxResults = 10;
query.setFirstResult(startIndex);
query.setMaxResults(maxResults);

I see this message in my log :

7 juin 2011 09:52:37 org.hibernate.hql.ast.QueryTranslatorImpl list
ATTENTION: firstResult/maxResults specified with collection fetch;
applying in memory!

I see the 200 result coming back (in log) and after in the HashSet i have finally the 10 result i ask.

its seems in memory is bring back the 200 result and after the maxResults is applied in memory.

I’m searching if there is any way to be able to fetch and limit the number of result.

I used a workaround, I make a first query to ask the id of my order , without any fetch, used the maxResult.
everything work perfectly it’s used the limit instruction.
After I use my “big” query with the fetch and limit the result inside the list of id bring back in the first one.

here it is my full query without my workaround (notice that there is no limit generated as talk by @Bozho ):

select o from Order  o
   left join fetch o.notes note
   left join fetch o.orderedBy orderedBy
   left join fetch orderedBy.address addressOrdered 
   left join fetch orderedBy.language orderedByLg 
   left join fetch orderedByLg.translations orderedByLgTtrad
   left join fetch o.deliveredTo deliveredTo 
   left join fetch deliveredTo.address addressDelivered 
   left join fetch deliveredTo.language deliveredToLg
   left join fetch deliveredToLg.translations 
   left join fetch o.finalReceiptPlace finalReceiptPlace
   left join fetch finalReceiptPlace.address addressFinalReceiptPlace 
   left join fetch finalReceiptPlace.language finalReceiptPlaceLg 
   left join fetch finalReceiptPlaceLg.translations
   inner join fetch o.deliveryRoute delivery
   left join fetch delivery.translations
   inner join fetch o.type orderType
   left join fetch orderType.translations 
   inner join fetch o.currency currency
   left join fetch currency.translations
   left join fetch o.attachments 
   left join fetch note.origin orig
   left join fetch orig.translations
   left join fetch o.supplier sup  
   left join fetch sup.department dep 
   left join fetch o.stateDetail stateD
   inner join fetch stateD.state stat  
where 1=1 and o.entryDate >= :startDat
  • 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-04T10:09:11+00:00Added an answer on June 4, 2026 at 10:09 am

    TL;DR Hibernate doesn’t know how many rows of the flattened, joined query it needs to get the specified number of the Order objects, so it has to load the whole query in memory. See below for an explanation.

    To understand why Hibernate does this, you need to understand how Hibernate does the ORM (Object-Relational Mapping) involved for JPA Entities.

    Consider a simplified set of Entities for your order. Class Order contains 2 fields: number and customerId and a list of order lines. Class OrderLine contains productCode and quantity fields, as well as a uid key and a reference to the parent Order.

    These classes may be defined thus:

    @Entity
    @Table(name = "ORDER")
    public class Order {
        @ID
        @Column(name = "NUMBER")
        private Integer number;
        @Column(name = "CUSTOMER_ID")
        private Integer customerId;
        @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
        @OrderBy
        private List<OrderLine> orderLineList;
    
        .... // Rest of the class
    }
    
    @Entity
    @Table(name = "ORDER_LINE")
    public class OrderLine
    {
        @ID
        @Column(name = "UID")
        private Integer uid;
        @Column(name = "PRODUCT_CODE")
        private Integer productCode;
        @Column(name = "QUANTITY")
        private Integer quantity;
        @Column(name = "ORDER_NUMBER")
        private Integer orderNumber;
        @ManyToOne(fetch = FetchType.LAZY, optional = false)
        @JoinColumn(name = "ORDER_NUMBER", referencedColumnName = "NUMBER", insertable = false, updatable = false)
        private Order order;
    
        .... // Rest of the class
    }
    

    Now, if you performed the following JPQL query on these Entities:

    SELECT o FROM Order o LEFT JOIN FETCH o.orderLineList
    

    then Hibernate performs this query as a ‘flattened’ SQL query similar to the following:

    SELECT o.number, o.customer_id, ol.uid, ol.product_code, ol.quantity, ol.order_number
    FROM order o LEFT JOIN order_line ol ON order_line.order_number = order.number
    

    which would give a result like this:

    | o.number | o.customer_id | ol.uid | ol.product_code | ol.quantity |
    |==========|===============|========|=================|=============|
    | 1        | 123           | 1      | 1111            | 5           |
    | 1        | 123           | 2      | 1112            | 6           |
    | 1        | 123           | 3      | 1113            | 1           |
    | 2        | 123           | 4      | 1111            | 2           |
    | 2        | 123           | 5      | 1112            | 7           |
    | 3        | 123           | 6      | 1111            | 6           |
    | 3        | 123           | 7      | 1112            | 5           |
    | 3        | 123           | 8      | 1113            | 3           |
    | 3        | 123           | 9      | 1114            | 2           |
    | 3        | 123           | 10     | 1115            | 9           |
    ...etc
    

    which Hibernate would use to ‘reconstruct’ Order objects with attached lists of OrderLine sub-objects.

    However, since the number of order lines per order is random, there is no way for Hibernate to know how many rows of this query to take to get the specified maximum number of Order objects required. So it has to take the whole query and build up the objects in memory until it has the right amount, before discarding the rest of the result set. The log warning it produces alludes to this:

    ATTENTION: firstResult/maxResults specified with collection fetch; applying in memory!
    

    We’re only just discovering now that these queries can have a significant impact on server memory use, and we’ve had issues with our server falling over with out of memory errors when these queries are attempted.

    By the way, I will say now that this is mostly just theory on my part and I have no idea how the actual Hibernate code works. Most of this you can glean from the logs when you have Hibernate logging the SQL statements it generates.


    UPDATE:
    Recently I have discovered a little ‘gotcha’ with the above.

    Consider a third Entity called Shipment which is for one or more Lines of an Order.

    The Shipment entity would have a @ManyToOne association to the Order entity.

    Let’s say you have 2 Shipments for the same Order which has 4 Lines.

    If you perform a JPQL query for the following:

    SELECT s FROM Shipment s LEFT JOIN s.order o LEFT JOIN FETCH o.orderLineList
    

    You would expect (or at least I did) to get 2 shipment objects back, each with a reference to the same Order object, which itself would contain the 4 Lines.

    Nope, wrong again! In fact, you get 2 Shipment objects, each referring to the same Order object, which contains 8 Lines! Yes, the Lines get duplicated in the Order! And yes, that is even if you specify the DISTINCT clause.

    If you research this issue here on SO or elsewhere (most notably the Hibernate forums), you’ll find that this is actually a feature not a bug, according to the Hibernate powers that be. Some people actually want this behaviour!

    Go figure.

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

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.