如何使用HQL删除?

时间:2014-05-23 18:20:26

标签: hibernate hql hql-delete

我正在使用这个例子: http://www.mkyong.com/hibernate/hibernate-query-examples-hql/

试图删除这种方式:

String qhl = "delete from logs  where dateTtime >= current_date - interval '1 month'";
Query query = session.createQuery(hql);
return query.executeUpdate();

但是我收到了这个错误:

ERROR: line 1:61: unexpected token: '1 month'
Mai 23, 2014 3:07:22 PM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:61: unexpected token: '1 month'
line 1:61: unexpected token: '1 month'
    at org.hibernate.hql.internal.antlr.HqlBaseParser.identPrimary(HqlBaseParser.java:4016)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:859)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3390)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3168)
...

尝试时:

String qhl = "delete from logs where dateTtime >= ( select cast(date_trunc('month', current_date) as date) )";

错误是:

logs is not mapped [delete from logs where dateTtime >= ( select cast(date_trunc('month', current_date) as date) )]

我做错了什么?

1 个答案:

答案 0 :(得分:1)

它无效的hql,只需使用hibernate的native query functionality代替供应商特定的数据库函数。