SQL org.hibernate.exception.SQLGrammarException:无法执行查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6899502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:32:23  来源:igfitidea点击:

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

sqlhibernate

提问by Natarajan Ranganathan

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

当我尝试使用 NamedQuery 获取列表时出现此异常:

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

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:

DAO 实现代码:

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

Please provide solution for this exception.

请提供此异常的解决方案。

回答by Dnavir

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.

有一段时间面临同样的问题,并发现问题是由于表名称与数据库中的类(或实体)名称不同。添加了 @Table(name = actual_table_name) 注释并且它起作用了。

回答by Ryan Stewart

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

获取 Hibernate 生成的 SQL 查询(使用hibernate.show_sql或者最好是 Hibernate 的SQL logging),然后自己对数据库执行它。这很可能有助于引导您朝着正确的方向前进。

回答by Angad Bansode

Try this one it could work. It Perfectly worked for me.

试试这个它可以工作。它非常适合我。

1) Class level annotation.

1) 类级注释。

@NamedQuery(name="UserDetails.byId" , query="from UserDetails where userId = ?")

2) Get record using NamedQuery

2) 使用 NamedQuery 获取记录

Query qry2 = sf.getCurrentSession().getNamedQuery("USER_DETAILS.byName")    ;
        qry2.setString(0, "Angad Bansode");
        List<UserDetails> user = qry2.list();
        for (UserDetails userDetails : user) {
            System.out.println("User Details by named native query  name = " + userDetails.getUserName() + ", aadhaar no  = " + userDetails.getAadharNo());
        }

回答by Angad Bansode

seems like this question is little old but any way once i added below line to hibernate config files it worked for me.

似乎这个问题有点老了,但是无论如何,一旦我在下面添加了一行来休眠配置文件,它就对我有用。

<property name="show_sql">true</property>