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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:02:56+00:00 2026-05-18T10:02:56+00:00

If I have entities like: @Entity public class Person { public String name; @OneToMany

  • 0

If I have entities like:

@Entity
public class Person {
   public String name;
   @OneToMany
   public Location location;
}

@Entity
public class Location {
   public String address;
   public String city;
   public String state;
}

Say I want to count all of the people in each state. Should I be able to do something like this:

Criteria criteria = session.createCriteria(Person.class);
criteria.setProjection(Projections.projectionList().
   add(Projections.groupProperty("location.state")).
   add(Projections.count("name")));
List<Object[]> result = criteria.list();

I know that exact example won’t work because I get an error saying, could not resolve property: location.state of of: Person. Is there a way to do what I’m trying to do without resorting to HQL? (The real version of this query is quite dynamic, so the programmatic building of the criteria is very helpful.)

  • 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-18T10:02:57+00:00Added an answer on May 18, 2026 at 10:02 am

    First, I created a toy example above, so it’s possible that the toy would have worked and even partenon’s suggestion might have worked on the toy example. The documentation seems to indicate that either should, but it didn’t work in my real scenario, which is too verbose to go into here.

    The issue is that it couldn’t find the location.state property. What ultimately worked was to create aliases in the criteria explicitly:

    criteria.createAlias("location", "location").createAlias("location.state", "state");
    

    I was still not able to use the “state” alias, but “location.state” then worked for me, so creating the aliases somehow primed the query builder to be able to find it when it couldn’t before. This all seems very hacky and inelegant, so I’m not proud of this solution, but it’s what worked. Does this sound like a bug to anyone else?

    For those curious, without going into the whole entity model, here is the actual criteria and projections that worked for me:

    obsCriteria.createAlias("phenomenon", "phenomenon").createAlias(
      "phenomenon.phenomenonGroup", "phenomenonGroup");
    ProjectionList projection = Projections.projectionList()
      .add(Projections.avg("value"))
      .add(Projections.groupProperty("phenomenon.phenomenonGroup"));
    obsCriteria.setProjection(projection);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have a Person and an Organisation Entity: Person looks like this: public class
Let's say I have the typical entity Car class Car : Entity { public
I have data that looks like this: entities id name 1 Apple 2 Orange
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
Say I have entities organized in a hierarchy with Parent being the root entity
I want to do something like this: @Entity public class Bar { @Id @GeneratedValue
I have a Linq to Entities query like this one: var results = from
I have a 3-leveled hierarchy of entities: Customer-Order-Line, which I would like to retrieve
When I have entities in my domain with lists of things, should they be

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.