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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:57:02+00:00 2026-06-13T21:57:02+00:00

In my web application I have an Object with a OneToMany relationship with Child.

  • 0

In my web application I have an Object with a OneToMany relationship with Child.
When selecting my Objects I execute the following query:

from Object o

and then i should print for every object how many children it has with

// Foreach
object.children.size()

Assuming the object has a lot of children (let’s say 30’000);
Is it a waste of resources calling size() or the ORM framework (in my case Hibernate) will take care of that without loading all the children?

  • 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-13T21:57:03+00:00Added an answer on June 13, 2026 at 9:57 pm

    Using JPA (the standard):

    • Your @OneToMany relationship is by default lazy-loaded (i.e. default value for fetch=FetchType.LAZY). But calling Entity.getCollection().size() would trigger lazy loading to retrieve all of the child collection – so yes, it would be fairly slow, unless you needed to operate on all/most elements anyway. Note: for all (sane) implementations of JPA, this will NOT issue 30,000 seperate queries – it will issue one query that returns 30,000 rows in the result set.
    • If you need most elements or you wanted to cache in advance change to @OneToMany(fetch=FetchType.EAGER)
    • The common way do obtain data statistics without retrieving every single object is simply to use JPQL via EntityManager.getQuery()/getTypedQuery()/getNamedQuery() (or even SQL via getNativeQUery()). This is quite simple and highly performant:
    int empSize = em.createQuery("SELECT SIZE(d.employees) FROM Department d")     
                     .getSingleResult();    
    
    OR ALTERNATIVELY
    
    // Pre-compiled query placed against entity class for highest performance   
    @NamedQueries({
        @NamedQuery(name="Department.EmployeeSize",
                    query="SELECT SIZE(d.employees) FROM Department"),
        ... // other named queries
    })
    @Entity
    public class Department {
    
        ...
    
    }
    
    // Then to use the query:    
    int empSize = em.createNamedQuery("Department.EmployeeSize", Integer.class)     
                     .getSingleResult();    
    
    • You can enable level 2 caching. JPA Caching Summary

      To statically configure level 2 caching:

    Map propertiesMap = new HashMap();
    // Valid values are ALL, NONE, ENABLE_SELECTIVE, DISABLE_SELECTIVE
    propertiesMap.add("javax.persistence.sharedCache.mode", "ENABLE_SELECTIVE");
    EntityManagerFactory = Persistence.createEntityManagerFactory("myPUName", propertiesMap);
    
    ALTERNATIVELY use persistence.xml:
    
    <persistence-unit name="EmployeeService">
        ...
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    
    </persistence-unit>
    

    Then mark which entities should be automatically cached in the level 2 cache:

    @Cacheable(true)
    @Entity
    public class Employee {
        ...
    }
    
    • Can also configure caching dynamically, as part of a particular query

    Using Proprietary approach (e.g. Hibernate “Extra-Lazy” collections):

    • Same performance as simply issuing JPQL/SQL query
    • Save a couple of lines of code (@org.hibernate.annotations.LazyCollection( EXTRA ) annotation v @NamedQuery annotation and execution)
    • Not standard – JPA trained developers won’t know about it
    • Not portable – can only be used with that vendor. There is an industry trend towards standard JPA, away from proprietary features. Many different JPA implementations are out there.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a web application and have been told that using object oriented
This is about asp.net mvc3 web application. We have used Object cache to store
I have a web application that uses the CDO Message object to email reports.
I have a web scraping application, written in OO Perl. There's single WWW::Mechanize object
I have a Python application in the bottle web-server that accesses a C shared-object
I'm working on a web application written on php. I have some objects (represented
I have a web application that adds contextual information to XmlHttpRequest objects using the
I have a web application which calls WCF service to get an object. I
I have a web application that uses an ActiveX COM component, for example: <OBJECT
When creating a web application, and lets say you have a User object denoting

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.