Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.hibernate.exception.SQLGrammarException: could not execute query?

Tags:

sql

hibernate

I got this exception when i try to get the list using NamedQuery:

org.hibernate.exception.SQLGrammarException: could not execute query

Below I mentioned code:

Entity Class Code:

@Table(name = "tbl_users")
@XmlRootElement
@NamedQueries({@NamedQuery(name = "TblUsers.findAll", query = "SELECT t FROM TblUsers t")});

DAO implement Code:

org.hibernate.Query query = session.getNamedQuery("TblUsers.findAll");
List list = query.list();

Please provide solution for this exception.

like image 275
Natarajan Ranganathan Avatar asked Aug 01 '11 13:08

Natarajan Ranganathan


2 Answers

Was facing same issue for a while and figured out that the problem was due to the Table name being different from the class(or Entity) name in the database. Added the @Table(name = actual_table_name) annotation and it worked.

like image 108
Dnavir Avatar answered Nov 05 '22 02:11

Dnavir


Get the SQL query that Hibernate is generating (using hibernate.show_sql or, preferably, Hibernate's SQL logging), and execute it against the database yourself. That will most likely help steer you in the right direction.

like image 28
Ryan Stewart Avatar answered Nov 05 '22 04:11

Ryan Stewart