使用数据库函数转换 hibernate 标准中的列
尽管用了 3 个小时的谷歌搜索和 API 搜索,我还是找不到任何关于是否可以在 Hibernate 标准查询中使用数据库函数的参考。具体来说:
我想访问 postgres 数据库中日期时间的日期部分并按其分组。我想象查询会类似于:
session.createCriteria(Exam.class)
.setProjection(Projections.projectionList()
.add(Projections.property("DATE(beginExam)").as("beginDate"))
.add(Projections.groupProperty("beginDate")))
.list();
这不起作用,给我一个“无法解析属性:Date(beginExam)...”异常。看起来这是一件非常简单的事情,我一定错过了一些东西。鉴于我也在动态地构建限制(我在示例中省略了它),似乎标准是用于此目的的休眠组件,但我愿意接受任何建议,但不回避整个问题通过建立我自己的小组。
谢谢
Despite 3 hours of googling and searching the API I can't find any reference to whether it's possible to use database functions within a hibernate criteria query. To be specific:
I'd like to access the date portion of the datetime in a postgres database and group by that. I'd imagine the query would look something like:
session.createCriteria(Exam.class)
.setProjection(Projections.projectionList()
.add(Projections.property("DATE(beginExam)").as("beginDate"))
.add(Projections.groupProperty("beginDate")))
.list();
This does not work giving me a "could not resolve property: Date(beginExam)..." exception. It seems like this is a very simple thing to do, and I must be missing something. Given that I am also building restrictions dynamically (I've left that out in the example) it seems that criteria is the hibernate component to use for this, but I'm open to any suggestions at this point short of side stepping the whole issue by building my own group by.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过 Projections.sqlProjection 像这样
或投影像这样的 .sqlGroupProjection
Hibernate 在 SQL 查询中生成表别名,因此您可能需要将
{alias}
片段添加到 SQL 片段中才能使其工作:或使用
SQLGroupProjection
:Have you tried Projections.sqlProjection like this
or Projections.sqlGroupProjection like this
Hibernate generates table aliases in SQL queries, so you may need to add the
{alias}
fragment to your SQL fragment to make this work:or with
SQLGroupProjection
:看一下 Projections.sqlProjection 和投影.sqlGroupProjection
Take a look at Projections.sqlProjection and Porjections.sqlGroupProjection