0

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks DainusI have one more query i.e if this query does not return any data, will query.list will be null or not null. If it is not null then if i do personDetailsData.get(0) will it be null or not null. (Because i have to to do some operations based on null or not null values)
If you call for list() it should not return null, but there's a special method for single object "getSingleResult()", if I good remember it throws exception when there is no result.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.