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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:20:26+00:00 2026-05-25T03:20:26+00:00

I have very simple data model like this: create table Company ( id int

  • 0

I have very simple data model like this:

create table Company (
    id int primary key,
    name varchar(50),
    street varchar(50)
)

create table Person (
    id int primary key,
    name varchar(50),
    surname varchar(50),
    id_company int foreign key references Company
)

corresponding java classes like this:

class Company {
    int id;
    String name, street;
    List<Person> employees;
    // getters, setters, ctor
}

class Person {
    int id;
    String name, surname;
    Company employer;
    // getters, setters, ctor
}

Now, I ‘d like to select Company with most employees with HQL. I don’t know how, here’s my rubbish pseudo attempt that doesn’t work:

from Company c having max(c.employees);
  • 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-25T03:20:27+00:00Added an answer on May 25, 2026 at 3:20 am

    EDIT: this worked for me – to get company id and employee count

    select c.id, max(c.employees.size) from Company c
    

    To get the company object:

    from Company comp
    where comp.employees.size = (
      select max(c.employees.size) from Company c
    )
    

    If you don’t have the hibernate plugin for eclipse yet install it it’s great for this as it also displays the generated sql so you can optimize your hql, see http://www.hibernate.org/subprojects/tools.html.

    Just thought of something, what happens if two companies CompA and CompB both have the highest number of employees, say 50, i.e. all other companies have less than 50, do you care which company id you get back? Are you more interested in the number 50 or the company id?

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

Sidebar

Related Questions

I have very simple select like this: SELECT * FROM table WHERE column1 IN
i have very simple problem. I need to create model, that represent element of
I have a very simple WPF application in which I am using data binding
I have a very simple Java RMI Server that looks like the following: import
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have hierarchical data stored in the datastore using a model which looks like
This is very simple, but I'm being rather stupid about it. I have a
I want my Data Model and my Getters and Setters set like a simple
I have very simple window where I have 2 buttons - one for cancel,
I have a very simple problem which requires a very quick and simple solution

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.