如何创建 Hibernate Criteria 以按集合的某些属性进行排序

发布于 2024-08-22 08:18:18 字数 165 浏览 6 评论 0原文

比如说,我有一个实体,它具有作为集合的操作历史。我想按最新操作的日期对实体进行排序(这是历史记录的第一个元素)。

我想做这样的事情:

criteria.addOrder(Order("history[0].date"))

这可能吗?

Say, i have an entity that has a history of operations as a collection. I want to sort entities by the date of the latest operation (it's the first element of history).

i'd like to do something like this:

criteria.addOrder(Order("history[0].date"))

is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓬勃野心 2024-08-29 08:18:18

据我所知,Criteria 仅支持按映射属性排序,因此可能将上次修改日期映射为计算属性(使用 属性标记的公式属性)就可以了。

诚然,这很丑陋,但由于到目前为止没有人发布更好的方法...

编辑:查询也可能执行得不太好,也许将其作为冗余列添加到实体表中是一个更好的选择。

As far as I know, Criteria only supports ordering by mapped properties, so perhaps mapping the date of last modification as computed property (using the formula attribute of the property tag) will do the trick.

Granted, this is ugly, but since nobody has posted a better approach so far ...

Edit: The query also might not perform too well, perhaps adding this as a redundant column to your entity table is a better option.

混吃等死 2024-08-29 08:18:18

根本不可能。

您的历史记录数据位于另一个表中,要按这种关系排序,您将需要更复杂的标准,至少涉及到另一个表的某种联接,我怀疑可能是别名。

发布您的类的映射,我们也许能够提示如何执行此操作。

Not at all likely.

The data for your history is in another table, and to order by this relation you're going to need more complex criteria at least involving some sort of join to the other table, and I suspect perhaps an alias.

Post the mapping of your class, and we might be able to give a hint of how to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文