0
votes

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

Here is the exception

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

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

2

2 Answers

3
votes

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

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

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.

1
votes

As you are using sql query so you have to create a sql query such as

sess.createSQLQuery("SELECT * FROM CATS").list();

see the source source