Mark
Mark

Reputation: 14930

Hibernate @OrderBy with referenced class

I have a class say: "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 mapping annotations:

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

How do I add an @OrderBy annotation to ClassA's collection to ClassB, so that the collection is ordered by the "name" property of ClassC

Like so:

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

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

Any help here would be awesome, as its really bugging me at the moment.

P.S. I should also mention that using the OrderBy annotation on the collection like this: @OrderBy(clause="classC asc") (i.e. without the .name on classC) I get a valid SQL statement, which uses the ID column (the primary key) of classC to order by.

Cheers, Mark

Upvotes: 6

Views: 19085

Answers (4)

use @Sort with custom comparator instead

Upvotes: 3

Atle
Atle

Reputation: 571

This is possible if you use JPA. See this article.

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

Upvotes: 1

ChssPly76
ChssPly76

Reputation: 100726

It's unfortunately impossible to do what you want. I've answered a similar question here.

@OrderBy only supports direct properties of the collection elements.

Upvotes: 8

Enyra
Enyra

Reputation: 17982

I only know NHibernate (.NET version), but I guess it should work similar:

@OrderBy(clause = "name asc")  

Or you can try this:

@OrderBy("name")  

Upvotes: 0

Related Questions