Hibernate异常查询未正确结束 [英] Hibernate exception Query not properly ended

查看:178
本文介绍了Hibernate异常查询未正确结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行此查询时获取异常

  select COUNT(tl.LOG_ID)AS EVTCOUNT,tl.PRIORITY FROM Customer_? tl其中tl.DEVICE_REPORTED_TIME> = SysDate -90 GROUP BY tl.PRIORITY ORDER BY tl.PRIORITY` 

这里我使用 query.setLong(0,custId);



所以它会变得像 Customer_1



如果我从SqlDeveloper IDE运行上述查询它工作正常或者如果我将此值设置为像< c $ c> Customer_1 而不是客户_?
它工作正常。



<错误:
$ b


WARN:org.hibernate.util.JDBCExceptionReporter - SQL错误:933,SQLState:42000
错误: org.hibernate.util.JDBCExceptionReporter - ORA-00933:SQL命令未正确结束


这个查询可能会出错,但其他查询运行正常吗?



编辑

我使用NamedQueries,这个查询在一个单独的xml文件中。

ou不能在这样的查询中使用参数。没有深入研究这个逻辑,你最好的选择就是在创建查询之前连接查询字符串。

  String queryString =select COUNT(tl.LOG_ID)AS EVTCOUNT,tl.PRIORITY FROM Customer_+ custId +tl where tl.DEVICE_REPORTED_TIME> = SysDate-90 GROUP BY tl.PRIORITY ORDER BY tl.PRIORITY; 
Query query = session.createSQLQuery(queryString);

编辑

对于NamedQueries,它们是在应用程序启动时编译的,因此我认为在运行时没有办法更改目标实体(表)。


Getting exception while running this query

select COUNT(tl.LOG_ID)AS EVTCOUNT,tl.PRIORITY FROM Customer_? tl Where tl.DEVICE_REPORTED_TIME >= SysDate-90 GROUP BY tl.PRIORITY ORDER BY tl.PRIORITY`

Here I am using query.setLong(0,custId);

so it will become like Customer_1

If I run the above query from SqlDeveloper IDE It is working fine or If I set this value as Statically like Customer_1 instead of Customer_? It working fine.

Errors:

WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 933, SQLState: 42000 ERROR: org.hibernate.util.JDBCExceptionReporter - ORA-00933: SQL command not properly ended

What Might be wrong with this query though other queries are running fine?

Edit

I am using NamedQueries and I have written this query in a separate xml file.

解决方案

You can't use a parameter in a query like that. Without going into the logic behind this, your best option is to concatenate the query string before creating a query

String queryString = "select COUNT(tl.LOG_ID) AS EVTCOUNT,tl.PRIORITY FROM Customer_" + custId + " tl Where tl.DEVICE_REPORTED_TIME >= SysDate-90 GROUP BY tl.PRIORITY ORDER BY tl.PRIORITY";
Query query = session.createSQLQuery(queryString);

EDIT

As for NamedQueries, they are compiled when the application is started so I don't think there is a way to change the target entity (table) at runtime.

这篇关于Hibernate异常查询未正确结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆