Search code examples
hibernatehqlhql-delete

How to delete using HQL?


I'm using this example: http://www.mkyong.com/hibernate/hibernate-query-examples-hql/

And trying to delete this way:

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

But I get this error:

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)
...

When trying this:

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

The error is:

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

What I'm doing wrong?


Solution

  • Its not valid hql, just use the native query functionality of hibernate instead, for vendor specific database functions.