oracle 使用引用类休眠 @OrderBy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1036902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:27:38  来源:igfitidea点击:

Hibernate @OrderBy with referenced class

oraclehibernateannotations

提问by Mark

I have a class say: "ClassA" which has a collection of "ClassB"

我有一堂课说:“ClassA”,它有一个“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:

“ClassB”有一个使用普通旧映射注释的映射类“ClassC”:

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

如何将@OrderBy 注释添加到ClassA 的集合到ClassB,以便该集合按ClassC 的“名称”属性排序

Like so:

像这样:

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

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

我得到的只是 Oracle 异常,说 classC 是未知的。

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.

PS 我还应该提到在集合上使用 OrderBy 注释是这样的:@OrderBy(clause="classC asc")(即在 classC 上没有 .name)我得到一个有效的 SQL 语句,它使用 ID 列(主要key) 的 classC 排序。

Cheers, Mark

干杯,马克

回答by ChssPly76

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

不幸的是,做你想做的事是不可能的。我在这里回答了一个类似的问题。

@OrderByonly supports directproperties of the collection elements.

@OrderBy仅支持集合元素的直接属性。

回答by Ji?í Vypěd?ík

use @Sortwith custom comparator instead

@Sort与自定义比较器一起使用

回答by Atle

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

如果您使用 JPA,这是可能的。请参阅这篇文章

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

然后你只需添加 @OrderBy("name")到集合属性

回答by Enyra

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

我只知道 NHibernate(.NET 版本),但我想它应该类似:

@OrderBy(clause = "name asc")  

Or you can try this:

或者你可以试试这个:

@OrderBy("name")