I have the following problem. I have three classes, A, B and C. A contains a OneToMany relationed list of B:s. B contains a ManyToOne relation to C. C contains a field called "name" and B also contains a field called "name". What I'd like to accomplish is to have the items in A's list sorted primarily by C's name and secondarily by B's name - the problem is that I do not know how to do this. Is it even possible?
我有以下问题。我有三个类,A,B和C. A包含一个关于B:s的OneToMany关系列表。 B包含与C的ManyToOne关系.C包含名为“name”的字段,B还包含名为“name”的字段。我想要完成的是让A列表中的项目主要按C的名称排序,其次按B的名称排序 - 问题是我不知道如何做到这一点。它甚至可能吗?
I'm using EclipseLink as my JPA provider.
我正在使用EclipseLink作为我的JPA提供程序。
class A {
@OneToMany
@OrderBy("b.c.name, b.name") <---- this is the problem
List<B> b;
}
class B {
@ManyToOne
C c;
String name;
}
class C {
String name;
}
EDIT Yes, I've tried different variations, for example @OrderBy("c.name") doesn't work, I just get an error message telling me that the entity class b does not contain a field called "c.name".
编辑是的,我尝试了不同的变体,例如@OrderBy(“c.name”)不起作用,我只是收到一条错误消息,告诉我实体类b不包含名为“c.name”的字段。
5 个解决方案
#1
It's NOT possible. @OrderBy only accepts direct property / field names, not nested properties. Which makes sense, really, because "c" table - depending on your fetching strategy may not even be part of a select issued to retrieve your "b"s.
这是不可能的。 @OrderBy只接受直接属性/字段名称,而不接受嵌套属性。这是有道理的,真的,因为“c”表 - 取决于你的提取策略甚至可能不是一个选择的一部分来检索你的“b”。
#2
ChssPly76 is right.
ChssPly76是对的。
What you could do is to create a named query like this one:
你可以做的是创建一个这样的命名查询:
SELECT b
FROM B b
WHERE b.a = :mya
ORDER BY b.c.name
#3
Have you tried @OrderBy("c.name", "name")
?
你试过@OrderBy(“c.name”,“name”)吗?
You shouldn't use "b." because it's implied that the @OrderBy will be done on columns of the instances of B on the b array.
你不应该使用“b。”因为暗示@OrderBy将在b数组的B实例的列上完成。
#4
Have you tried:
你有没有尝试过:
@OrderBy("c.name ASC", "name ASC")
?
#5
It is not possible in javax.persistence.OrderBy (as say ChssPly76 ), but when I was using Hibernate I construct new column in PLAIN SQL with Formula() annotation and then OrderBy over it:
在javax.persistence.OrderBy(如ChssPly76)中是不可能的,但是当我使用Hibernate时,我在PLAIN SQL中使用Formula()注释构建新列,然后使用OrderBy:
class A {
@OneToMany
@OrderBy("orderCol") <---- reference to virtual column
List<B> b;
}
class B {
@ManyToOne
C c;
String name;
@org.hibernate.annotations.Formula(
"( select C_table.name as orderCol from C_table where C_table.id = id )"
) <------------------------------------------ join with plain SQL statment
String orderCol;
}
May be EclipseLink has same possibilities?
可能是EclipseLink有相同的可能性吗?
#1
It's NOT possible. @OrderBy only accepts direct property / field names, not nested properties. Which makes sense, really, because "c" table - depending on your fetching strategy may not even be part of a select issued to retrieve your "b"s.
这是不可能的。 @OrderBy只接受直接属性/字段名称,而不接受嵌套属性。这是有道理的,真的,因为“c”表 - 取决于你的提取策略甚至可能不是一个选择的一部分来检索你的“b”。
#2
ChssPly76 is right.
ChssPly76是对的。
What you could do is to create a named query like this one:
你可以做的是创建一个这样的命名查询:
SELECT b
FROM B b
WHERE b.a = :mya
ORDER BY b.c.name
#3
Have you tried @OrderBy("c.name", "name")
?
你试过@OrderBy(“c.name”,“name”)吗?
You shouldn't use "b." because it's implied that the @OrderBy will be done on columns of the instances of B on the b array.
你不应该使用“b。”因为暗示@OrderBy将在b数组的B实例的列上完成。
#4
Have you tried:
你有没有尝试过:
@OrderBy("c.name ASC", "name ASC")
?
#5
It is not possible in javax.persistence.OrderBy (as say ChssPly76 ), but when I was using Hibernate I construct new column in PLAIN SQL with Formula() annotation and then OrderBy over it:
在javax.persistence.OrderBy(如ChssPly76)中是不可能的,但是当我使用Hibernate时,我在PLAIN SQL中使用Formula()注释构建新列,然后使用OrderBy:
class A {
@OneToMany
@OrderBy("orderCol") <---- reference to virtual column
List<B> b;
}
class B {
@ManyToOne
C c;
String name;
@org.hibernate.annotations.Formula(
"( select C_table.name as orderCol from C_table where C_table.id = id )"
) <------------------------------------------ join with plain SQL statment
String orderCol;
}
May be EclipseLink has same possibilities?
可能是EclipseLink有相同的可能性吗?