Hibernate HQL:将Datetime转换为Date?

时间:2017-05-24 08:38:16

标签: hibernate datetime hql

在SELECT查询中,我想将我的DATETIME列转换为DATE格式(仅使用年,月和日)。

我正在使用DATE构造:DATE(table.column),它很棒,但似乎这个构造不适用于SQLSERVER数据库(我之前正在使用MySql)。

这是我遇到的错误:

 data type for node: org.hibernate.hql.ast.tree.MethodNode +-[METHOD_CALL] MethodNode: '(' | +-[METHOD_NAME] IdentNode: 'DATE'

有人可以帮我解决这个问题吗?我尝试使用CONVERT和CAST,但我遇到了同样的错误。

这是查询:

SELECT DATE(al.loginTime) as loginTime, (COUNT(al.hawkUser)) as numeroAccessi, al.hawkUser as hawkUser, al.logoutTime as logoutTime " +
                "FROM AccessLog as al GROUP BY al.hawkUser, DATE(al.loginTime) ORDER BY DATE(al.loginTime) DESC, al.hawkUser DESC 

1 个答案:

答案 0 :(得分:0)

好的,我多次修改了查询,现在是这样的:

 String query="SELECT "
        + "  al.loginTime as loginTime,"
        + " (COUNT(hu.id)) as numeroAccessi, "
        + " hu.id as hawkUser "
                + "FROM AccessLog as al "
                + " INNER JOIN al.hawkUser hu "
                + " GROUP BY "
                + " al.loginTime,"
                + " hu.id "
                + " ORDER BY "
                + " al.loginTime DESC"
                + " hu.email  ";

loginTime是DATETIME,有小时,分钟和秒,而我只需要使用年月和日(它是登录列表)重新组合它们。所以我仍然需要将DATETIME转换为DATE。我无法使用CAST和CONVERT,因为此查询适用于MySql和SQLServer数据库,并且两者的语法都不同。