使用弹簧运行HQL时出现未知错误消息

时间:2013-09-07 19:38:58

标签: hibernate spring-mvc

我想在这里运行这件事。

public List<String> getOnlineUsers(){
        List<User> d=sessionfactory.getCurrentSession().createQuery("from User").list();
        List<String> onlineUsers=null;
        List<Integer> timediff=null;
        for(int i=0;i<d.size();i++){
            timediff=(List<Integer>)sessionfactory.getCurrentSession().createQuery("SELECT TIMESTAMPDIFF(SECOND,'"+d.get(i).getLastUpdated()+"',now())").list();
        }
        for(int i=0;i<timediff.size();i++){
            System.out.println("time "+timediff.get(i));
            if(timediff.get(i)<=120){
                onlineUsers.add(d.get(i).getUsername());
            }
        }
        return onlineUsers;

    }

但我收到此错误消息

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.beingjavaguys.controller.HomeController.showOnlineUsers(java.lang.String,org.springframework.validation.BindingResult)]; nested exception is java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode 
 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'TIMESTAMPDIFF' {originalText=TIMESTAMPDIFF}
    \-[EXPR_LIST] SqlNode: 'exprList'
       +-[IDENT] IdentNode: 'SECOND' {originalText=SECOND}
       +-[QUOTED_STRING] LiteralNode: ''2013-09-08 01:02:11.0''
       \-[METHOD_CALL] MethodNode: '('
          +-[METHOD_NAME] IdentNode: 'now' {originalText=now}
          \-[EXPR_LIST] SqlNode: 'exprList'

甚至不能在错误信息上思考。我看到这一刻的时候我一片空白。我想出来的原因是什么?

1 个答案:

答案 0 :(得分:1)

我认为您必须在TIMESTAMPDIFF中传递有效的日期时间而不是文字作为第二个参数。