Hibernate @ManyToMany joinTable - OrderBy使用连接表的字段 [英] Hibernate @ManyToMany joinTable - OrderBy using join table's field
问题描述
TABLE_A
ID_A
field1
fieldN
TABLE_B
ID_B
field1
fieldN
/ b
TABLE_A_B
ID_A
ID_B
orderField
public class A(){
@ManyToMany
@JoinTable(name =TABLE_A_B,joinColumns = {@JoinColumn(name =ID_A)},inverseJoinColumns = {@ JoinColumn(name =ID_B)})
@OrderBy(value =orderField)
private List< TABLE_B> BList;
}
但它不起作用,而是出现运行时错误: p>
原因:org.postgresql.util.PSQLException:错误:列B1_.orderField不存在位置:1437
Hibernate搜索TABLE_B中的字段。是否可以在使用连接表字段的多对多关系中使用OrderBy?或者任何其他方式在同一个领域进行订单?
您的代码是正确的,您是否尝试过,它将工作。一个问题是你正在使用 list
,所以当你在缓存中拥有该列表时可能不会被排序,但是如果你清除当前会话并再次获取它,它将被排序如你设置 @OrderBy(value =orderField)
。
另外还有一个问题, c $ c> @OrderBy 关于休眠文档;
列表可以以两种不同的方式进行映射:
- 作为有序列表,其中订单未在数据库中实现
- 作为索引列表,其中订单在数据库中实现
li>
要在内存中排序列表,请将@ javax.persistence.OrderBy添加到
属性中。
但是我刚刚尝试过您的问题,并且在数据库上的查询按参数排序( hibernate.show_sql = true
),所以我不确定他们的意思与上面的陈述。
我的查询示例;
select ... from .. 。inner join ... where users0_.event_id =? order by user1_.fullname
我会得出结论,如果订购是在数据库上完成的,那么您可以安全地使用@OrderBy注释。
$ b $ p 更新:
@例如,OrderBy
应该有原生的sql值;
@OrderBy(value =orderField)
私人列表< TABLE_B> BList;
其中 orderField
是列的数据库名称在表 TABLE_B
中,也可以这样做 @OrderBy(value =lower(fullname)desc)
如果你想创建连接TABLE_A_B中的列,它与java中的列表保持相同的顺序,那么你应该使用 @OrderColumn
code> @ManyToMany
@JoinTable(name =TABLE_A_B,joinColumns = {@ JoinColumn(name =ID_A)},inverseJoinColumns = {@ JoinColumn(name =ID_B)})
@OrderColumn(name =orderField)
私人列表< TABLE_B> BList;
There are 3 tables:
TABLE_A
ID_A
field1
fieldN
TABLE_B
ID_B
field1
fieldN
TABLE_A_B
ID_A
ID_B
orderField
public class A(){
@ManyToMany
@JoinTable(name="TABLE_A_B", joinColumns={@JoinColumn(name="ID_A")}, inverseJoinColumns={@JoinColumn(name="ID_B")})
@OrderBy(value="orderField")
private List<TABLE_B> BList;
}
But it isn't working, instead I get a runtime error:
Caused by: org.postgresql.util.PSQLException: ERROR: column B1_.orderField doesn't exist Position: 1437
Hibernate searches the field in TABLE_B. Is it possible to use "OrderBy" in a many to many relationship using a join table's field? Or any other way to get to order based in the same field?
Your code is correct, did you try that, it will work. One problem is that you are using list
so while you having that list in cache probably wouldn't be sorted, but if you clear current session and fetch it again it will be ordered as you set with @OrderBy(value="orderField")
.
Also there is another issue with @OrderBy
regarding to a hibernate documentation;
Lists can be mapped in two different ways:
- as ordered lists, where the order is not materialized in the database
- as indexed lists, where the order is materialized in the database
To order lists in memory, add @javax.persistence.OrderBy to your property.
But I've just tried your problem and query on database has order by parameter (hibernate.show_sql=true
), so I'm not sure what they meant with upper statement.
My query example;
select ... from ... inner join ... where users0_.event_id=? order by user1_.fullname
I'll conclude, if ordering is done on database then you can safely use @OrderBy annotation.
UPDATE:
@OrderBy
should have native sql value, for example;
@OrderBy(value="orderField")
private List<TABLE_B> BList;
where orderField
is database name of the column in table TABLE_B
, also it is possible to do this @OrderBy(value="lower(fullname) desc")
If you want to create column in join TABLE_A_B which keeps same order as it is in your list in java, then you should use @OrderColumn(name="orderField")
Try this;
@ManyToMany
@JoinTable(name="TABLE_A_B", joinColumns={@JoinColumn(name="ID_A")}, inverseJoinColumns={@JoinColumn(name="ID_B")})
@OrderColumn(name="orderField")
private List<TABLE_B> BList;
这篇关于Hibernate @ManyToMany joinTable - OrderBy使用连接表的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!