获取org.hibernate.hql.ast.QuerySyntaxException,尽管它可以正常工作的sql server?

获取org.hibernate.hql.ast.QuerySyntaxException,尽管它可以正常工作的sql server?

问题描述:

我有以下代码片段.它会在第3行处引发异常,但查询可以在Managed Studio Studio(SQL Server 2005)中正常运行

i have below code snippet. It throws the exception at line 3 but query works fine managemnt studio(sql server 2005)

String query = "select * from user where userId=" + profileId
    + " and spaceName='" + spaceName + "'";

Session session = HibernateUtil.getSession();

List<PersonDetailsData> personDetailsData = new ArrayList<PersonDetailsData>(
    session.createQuery(query).list()); //line 3

这是个例外

org.hibernate.hql.ast.QuerySyntaxException:意外令牌:*附近 第1行,第8列[从userId = 216的用户中选择*, spaceName ='DIG']

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from user where userId=216 and spaceName='DIG']

当管理sudio中的查询运行正常时,我无法弄清楚查询出了什么问题?

I am not able to figure out what's the problem with query when it is running fine in management sudio?

这是本机查询,不是hql. 如果您已将表字段映射到类字段,则需要

It's native query, not hql. If you have mapped table field to class fields you need

session.createSQLQuery(query, PersonDetailsData.class).list();

或创建hql类型查询-

or create hql type query -

select p from PersonDetailData p where p.userId = :userId and p.spaceName =:spaceName

并在查询中使用参数,而不是直接值.

and use parameters in query, not direct values.