Hibernate @OrderBy with reference class

I have a class that says: "ClassA", which has a collection of "ClassB"

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "COLUMN_NAME") private List<ClassB> lotsOfClasses; 

"ClassB" has a mapped class "ClassC" using plain old display annotations:

 public class ClassB { ... @ManyToOne @JoinColumn(name="AD_POINT_ID") private ClassC classC; ... } 

How to add the @OrderBy annotation to the ClassA collection to ClassB, so that the collection is ordered by the "name" property of the ClassC class

Same:

 @OrderBy(clause="classC.name asc") 

All I get are Oracle exceptions saying that classC is unknown.

Any help here would be awesome as it really bothered me at the moment.

PS It should also be mentioned that using the OrderBy annotation in the collection: @OrderBy (clause = "classC asc") (that is, without .name on classC) I get a valid SQL statement that uses the column ID (primary key) of class C for ordering.

Cheers, Mark

+6
oracle annotations hibernate
source share
4 answers

Unfortunately, it is impossible to do what you want. I answered a similar question here .

@OrderBy only supports the direct properties of collection items.

+7
source share

use @Sort instead of custom comparator

+3
source share

This is possible if you use JPA. See Article.

Then you simply add @OrderBy("name") to the collection property

+1
source share

I only know NHibernate (the .NET version), but I think it should work similarly:

 @OrderBy(clause = "name asc") 

Or you can try the following:

 @OrderBy("name") 
-one
source share

All Articles