orderBy 在集合字段上的用途?
我尝试从数据库中检索有序的元素列表。据我了解, @orderBy 注释应该足够了。
可以注释两个地方:
表示集合的字段
@OrderBy
@OneToMany(cascade = CascadeType.ALL, mappedBy = Entry.REPORT_PROP)
private List<Entry> elements
getter
@OrderBy
public List<Entry> getElements()
问题介绍
如果 field 和 getter 都被注释,则检索到的列表的顺序正确。但是,如果仅注释字段,则检索到的列表将使用数据库自然排序。
问题
仅注释字段有什么作用吗?如果不是,为什么可以只注释字段?
I tried to retrieve ordered list of elements from the database. From what I understand, @orderBy annotation should be sufficient.
It is possible to annotate two places:
field representing collection
@OrderBy
@OneToMany(cascade = CascadeType.ALL, mappedBy = Entry.REPORT_PROP)
private List<Entry> elements
getter
@OrderBy
public List<Entry> getElements()
The problem intro
If both field and getter are annotated, the retrieved list is correctly ordered. However, if only field is annotated, the retrieved list is using database natural ordering.
The question
Does annotating only field do anything? And if not, why is it possible to annotate only field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将注释放在哪里并不重要。根据您注释的方式,应该存在三种情况:
应该意味着在没有任何特定顺序的情况下检索列表(您的 SQL 数据库可能会以某种有序形式返回它们,但根据标准,如果不这样做是完全合法的) t)。
应检索按主键排序的条目,并
应检索按字段名升序排序的条目,对应于以下 JPQL 语句:
It shouldn't matter where you put the annotation. Depending on how you annotate, there should be three cases:
should mean that the list is retrieved without any particular order (your SQL database will probably return them in some ordered form, but according to the standard, it is perfectly legal if it doesn't).
should retrieve the entries ordered by primary key, and
should retrieve the entries ordered by fieldname in ascending order, corresponding to the following JPQL statement: