如何创建 Hibernate Criteria 以按集合的某些属性进行排序
比如说,我有一个实体,它具有作为集合的操作历史。我想按最新操作的日期对实体进行排序(这是历史记录的第一个元素)。
我想做这样的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,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.
根本不可能。
您的历史记录数据位于另一个表中,要按这种关系排序,您将需要更复杂的标准,至少涉及到另一个表的某种联接,我怀疑可能是别名。
发布您的类的映射,我们也许能够提示如何执行此操作。
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.