我要在几小时内区分当前日期和数据库中存储的日期。在PostgreSQL中,我可以使用以下查询来完成它:
SELECT storage_time, extract(epoch from (localtimestamp - storage_time))/3600 as diff_hour FROM some_table;
这给了我以小时为单位的日期差异:
storage_time | diff_hour
-------------------------+-------------------
2014-02-03 19:37:39.481 | 44.4788805811111
2014-02-03 19:40:28.201 | 44.4320139144444
2014-01-29 18:25:12.828 | 165.686284192222
2014-02-03 19:25:56.861 | 44.6740528033333
2014-02-05 15:53:38.178 | 0.212575858888889
2014-01-30 15:53:38.61 | 144.212455858889
现在,我很难将其移植到Hibernate查询语言。我尝试了很多变化,但是我无法从日期差异中获得 epoch 。这肯定不起作用:
"FROM SomeEntity " +
"WHERE someAttribute = :attr " +
"AND epoch(localtimestamp - deviceInfo.storageTime) / 3600 > :threshold_hour";
当我运行此命令时,我得到以下异常:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2040)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1837)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
at org.hibernate.loader.Loader.doQuery(Loader.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doList(Loader.java:2526)
at org.hibernate.loader.Loader.doList(Loader.java:2512)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
at org.hibernate.loader.Loader.list(Loader.java:2337)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1275)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
我像这样运行查询:
Query query = sessionFactory.getCurrentSession()
.getNamedQuery(GET_NON_EXPIRED_ENTITY)
.setString("attr", attr)
.setInteger("threshold_hour", thresholdHour);
List<SomeEntity> instances = query.list();
当我拨打list()
时,第2行会出现例外情况。
但是,当我将epoch()
函数更改为day()
时,在hibernate查询中,它会毫无例外地运行,但当然会产生意外结果。
有什么方法可以在HQL中获得PostgreSQL查询的相同行为吗?
答案 0 :(得分:1)
答案 1 :(得分:-1)
你可以尝试
"AND DATEDIFF('hour',deviceInfo.storageTime, localtimestamp ) > :threshold_hour";