在hibernate

时间:2018-01-15 10:12:28

标签: spring hibernate

我有一个与我的服务通信的控制器,以获取登录用户的ID。我首先使用主对象

获取用户名
@ResponseBody
    @RequestMapping(value = "/lid",method = RequestMethod.GET)
    public Integer lid(Principal p) {
        String name = p.getName();
        Integer gotid = personService.getDbId(name);
        return gotid;   

}

然后调用将用户名传递给dao的服务

public Integer getDbId(String name){
        Session currentSession = sessionFactory.getCurrentSession();
        @SuppressWarnings("rawtypes")
        Query theQuery = currentSession.createQuery("select id from app_user where sso_id=:name");
        //set multiple parameters from the user
        //theQuery.setParameter("city", city);
        theQuery.setParameter("sso_id", name);
        //Get the int or the big int
        //Get results into list object : List list = query.list();
        int theid = (int)theQuery.getSingleResult();
        //uniqueResult
        return theid;
        //org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: from near line 1, column 8 [select from app_user where sso_id=:name]
    }

然而,我收到此错误

  

java.lang.IllegalArgumentException异常:   org.hibernate.hql.internal.ast.QuerySyntaxException:app_user不是   映射[从app_user中选择id,其中sso_id =:name]

我如何修复它以给我一个我想要的结果?

2 个答案:

答案 0 :(得分:0)

您正在尝试执行纯SQL,但hibernate需要HQL。所以它试图找到一个app_user名称但却失败的实体 - 没有这样的类。

尝试改为

Query theQuery = currentSession.createSQLQuery("...");

答案 1 :(得分:0)

试试这个

Query theQuery = currentSession.createQuery("select id from AppUser where ssoId=:name");

AppUser可能是您的类名,而ssoId是字段名。

使用您的域类名和域字段名称,因为查询需要HQL。