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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:59:21+00:00 2026-06-12T13:59:21+00:00

I have a hibernate object called User which has a many-to-many relationship with another

  • 0

I have a hibernate object called User which has a many-to-many relationship with another hibernate object called Post. The relationship is called likedPosts (from the User perspective).

<set name="likedPosts" lazy="true" table="Likes" where="Deleted=0">
        <cache usage="read-write"/>
        <key column="UserID"/>
        <many-to-many column="PostID" class="Post"/>
    </set>

And currently I have a simple HQL query to return the list of liked posts as follows:

"select user.likedPosts from User user where user.id=:uid"

What I would like is to have this list ordered by a property in the Post object (specifically the post id). My (naive) attempt to do this was this:

"select user.likedPosts from User user where user.id=:uid 
        order by user.likedPosts.id desc"

This doesn’t work though – I get an exception telling me I’m not allowed to do this. How would I go about doing this? Thanks!

Stack trace for error:

org.hibernate.QueryException: illegal attempt to dereference collection [user0_.ID.likedPosts] with element property reference [id] [select user.likedPosts from com.pashash.domain.User user where user.id=:uid order by user.likedPosts.id desc]
    at org.hibernate.hql.ast.tree.DotNode$1.buildIllegalCollectionDereferenceException(DotNode.java:46)
    at org.hibernate.hql.ast.tree.DotNode.checkLhsIsNotCollection(DotNode.java:513)
    at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:221)
    at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
    at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:90)
    at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:728)
    ...
  • 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-12T13:59:22+00:00Added an answer on June 12, 2026 at 1:59 pm

    You need an explicit join to do that:

    select post from User user
    inner join user.likedPosts post
    where user.id = :userId
    order by post.id desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an object, A, which contains another object, B. We have Hibernate calling
I have a JPA/Hibernate object which uses a composite key as the primary key.
I have a spring-hibernate application which is failing to map an object properly: basically
I have a tree-like object, managed by Hibernate. The object has a list of
I have a class called SynonymMapping which has a collection of values mapped as
I have 2 Hibernate objects Group and User which are related with a ManyToMany
Hi I have an object called document and one called user Document public virtual
I'm using Spring Roo which generated set of hibernate and FlexJSON classes. I have
I'm using Hibernate/JPA and have an @Entity object called Order, pointing at a MySQL
I have a hibernate object that gets detached and transferred to a thick java

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.