David542
David542

Reputation: 110163

How to get result set in Hibernate

I am trying to do the following in hibernate:

Session session2 = HibernateUtil.getSessionFactory().openSession();
List<TitleEntity> tt = session2.createQuery(
  "select * from title where url = 'http://google.com'
").getResultList();

However, I get the following error:

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [select * from title where url = 'http://google.com']
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:670)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103)

What would be the correct way to get SELECT * as a result set?

Upvotes: 0

Views: 1090

Answers (1)

John Ericksen
John Ericksen

Reputation: 11113

In HQL you don't specify the * character, in fact you don't need to specify select unless you want to only return a portion of the query. Also you need to reference the entity name specifically. So removing select * and updating the entity name fixes your query:

from TitleEntity where url = 'http://google.com'

The error indicates the 8th column being the culprit, this directly indicates the * character of the query.

For reference: https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html#queryhql-select

Upvotes: 1

Related Questions