Hibernate QuerySyntaxException

时间:2015-04-20 18:15:44

标签: java mysql sql database hibernate

我收到了异常

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: dayofweek near line 1, column 155

以下是查询表格的函数

    Session session = getSession();
    Query query = session.createQuery("select count(tex.task.id)" + " from " + TestExecution.class.getName() + " tex where tex.userId=:userId and " +
            "tex.executedAt >= curdate()- interval dayofweek(curdate())+6 day and tex.executedAt < curdate() - interval dayofweek(curdate())-1 DAY");
    query.setParameter("userId", userId);
    return (long) query.uniqueResult();

请帮帮我..

1 个答案:

答案 0 :(得分:1)

您需要使用createSQLQuery方法:

Session session = getSession();
Query query = session.createSQLQuery("select count(tex.task.id)" + " from " + TestExecution.class.getName() + " tex where tex.userId=:userId and " +
        "tex.executedAt >= curdate()- interval dayofweek(curdate())+6 day and tex.executedAt < curdate() - interval dayofweek(curdate())-1 DAY");
query.setParameter("userId", userId);
return (long) query.uniqueResult();