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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:59:25+00:00 2026-06-17T12:59:25+00:00

I have two entities Customer and Order in a one-to-many relation. For each customer

  • 0

I have two entities Customer and Order in a one-to-many relation.
For each customer I need to count the number of associated orders and sort the results by this number.
In a native postgres query it looks like this:

select cust.id, count(order.id) from customers cust
left outer join orders order
on cust.id = order.customer_id
where .... conditions ...
group by cust.id
order by count desc;

But I must do this using CriteriaBuilder because this query is part of a larger piece of code that uses CriteriaBuilder to put in additional conditions. In Hibernate I would have probably used Projections, but I can’t find anything similar in JPA.

Any help in composing the query using CriteraBuilder would be much appreciated.

Thank you in advance.

  • 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-17T12:59:26+00:00Added an answer on June 17, 2026 at 12:59 pm

    Supposing that the entity Customer has a OneToMany property like this:

    @OneToMany(mappedBy = "customerId")
    private Collection<Order> orders;
    

    You can use the following query:

    EntityManager em;  // to be built or injected
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> cq = cb.createTupleQuery();
    Root<Customer> customer = cq.from(Customer.class);
    CollectionJoin<Customer, Order> orders = customer.join(Customer_.orders, JoinType.LEFT);
    cq.select(cb.tuple(customer, cb.count(orders)));
    cq.where(... add some predicates here ...);
    cq.groupBy(customer.get(Customer_.id));
    cq.orderBy(cb.desc(cb.count(orders)));
    List<Tuple> result = em.createQuery(cq).getResultList();
    for (Tuple t : result) {
        Customer c = (Customer) t.get(0);
        Long cnt = (Long) t.get(1);
        System.out.println("Customer " + c.getName() + " has " + cnt + " orders");
    }
    

    The above approach uses Metamodel. If you don’t like it, you can replace Customer_.orders with "orders" and Customer_.id with "id".

    If the OneToMany property is of another type, replace CollectionJoin with the collection of the proper type (ListJoin, SetJoin, MapJoin).

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

Sidebar

Related Questions

I have two entities: parent Customer and child Order . Each Customer has 1,000,000
I have two entities say Customer and Order which exist on their own and
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many
I have two entities, Customer and Order, both of which I have created types
i have two entities named Parent and Child , linked in a one-to-many relationship.
I have two entities that usually have one-to-many relationship, but in rare cases should
I have two entities, Job and Language , in a many-to-one relationship. The mapping
i have two entities Customer and Order (trivial setters and getters excluded) @Entity public
I have two entities referenced one to many. When entity framework created the table
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{

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.