views:

1415

answers:

3

Hi All,

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

A: 

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

@OrderBy(clause = "name asc")

Or you can try this:

@OrderBy("name")
Enyra
But that would be the 'name' property of ClassB, I need the name property of ClassC
Mark
A: 

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.

ChssPly76
A: 

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

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

Atle