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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:38:14+00:00 2026-05-18T01:38:14+00:00

Is it possible to order a collection of parent objects by both a parent’s

  • 0

Is it possible to order a collection of parent objects by both a parent’s property and a child’s property?

Let’s say I have an Entity class that has this bit of code:

@OrderBy("age")
public List<Father> getFathers() {
    return fathers;
}

The Father Entity has an age property which we want to order by ascending age (as shown above).

A Father can have a Child. A Child would also have an age property. I would like getFathers() to return a Fathers list such that they are ordered by the Fathers’ age and then subsequently the Child’s age ascending.

If I keep the code above as is, the Fathers would be ordered by their own age correctly but a Father (aged 30) with a 10 year old Child could come before a Father (also aged 30) with a 5 year old Child.

I tried doing something like this:

@OrderBy("age, child.age")
public List<Father> getFathers() {
    return fathers;
}

But I would get the following exception:

org.hibernate.AnnotationException: property from @OrderBy clause not found: Father.child.age

Edit: Sorry, let me clarify with a bit more code. The getFathers() is not in the Child entity but in another entity. Let’s call it Village:

@Entity
public class Village {
    ...
    @OneToMany(mappedBy = "village",
               targetEntity = Father.class)
    @OrderBy("age")
    public List<Father> getFathers() {
        return fathers;
    }
}

So let’s say there is a Village and you can obtain the Fathers that live in this Village. For this example’s sake, let’s say that a Father for some odd Village rule can only have one child each.

So Father would look something like this:

@Entity
public class Father {
    private Child child;
    ...

    @OneToOne(mappedBy = "father")
    public Child getChild() {
        return child;
    }

    public void setChild(Child child) {
        this.child = child;
    }
}

So now in the original Village class, I want getFathers() to return all the fathers in order of age, plus, if there are any fathers with the same age, I want them to be then ordered by the age of their child.

Edit 2: I’ve also found that I can get the effect of what I want however it can’t be the most elegant way of doing it:

session.createFilter(village.getFathers(), "order by child.age");
  • 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-18T01:38:15+00:00Added an answer on May 18, 2026 at 1:38 am

    It seems like you are missing two-way mapping. You should also map Child in Father.

    By the way, there should be one-to-many relationship between Father and Child, traditionally speaking. Which means in Father class there should be a List<Child> children; property, and not the other way around.

    [EDIT]

    No, you can’t do like that. Why? Because when you define your @OrderBy("age, child.age"), the whole of it, child.age, would be considered as a property. It will be treated as a String. Hence, indeed, you don’t have any property name child.age in Father class. I hope you are getting my point. So, the Filter solution, you came up with, is a good way to go.

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

Sidebar

Related Questions

I have a private class variable that holds a collection of order items: Private
I have a parent Obj-C object containing (in a collection) a bunch of objects
Is it possible to retrieve items from a Python dictionary in the order that
Is it possible to order a MongoDB collection by the size of a nested
Is it possible to order results in SQL Server 2005 by the relevance of
Is it possible to do a SELECT statement with a predetermined order, ie. selecting
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
I am working on application to keep a comic book collection in order. The
I was wondering if it is possible to remember the order in which rows
Lets say I have a folder with the following jpeg-files: adfjhu.jpg Afgjo.jpg Bdfji.jpg bkdfjhru.jpg

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.