这适用于Oracle,但我们已被告知切换到MySQL。更改驱动程序信息和其他相关设置以指向MySQL数据库后,我无法再执行我的命名查询。这是实体中定义的最简单的一个:
@NamedQuery(name =“get_capability”,query =“从Capability cb中选择cb,其中cb.financial_Id =:fiId”)
然后我有代码来执行查询,如下所示:
query = em.createNamedQuery(“get_capability”);
。 。
query.setParameter(“fiId”,fiId);
query.setMaxResults(1);
cabability =(Capability)query.getSingleResult();
最后一行抛出异常:
2013-04-24 10:46:00,677 WARN [org.hibernate.util.JDBCExceptionReporter](http-127.0.0.1-8080-1)SQL错误:1248,SQLState:42000
2013-04-24 10:46:00,677 ERROR [org.hibernate.util.JDBCExceptionReporter](http-127.0.0.1-8080-1)每个派生表必须有自己的别名
生成的SQL记录在日志文件中,如下所示:
/* named HQL query get_capability */ select
*
from
( select
capability0_.FINANCIAL_ID as FINANCIAL1_272_,
capability0_.ACTIVE_CONNECTIONS as ACTIVE2_272_,
capability0_.ALLOWED_CONNECTIONS as ALLOWED3_272_,
capability0_.COMPLETE_DETAILS as COMPLETE4_272_,
capability0_.FI_NAME as FI5_272_,
capability0_.MESSAGE_FORMAT as MESSAGE6_272_,
capability0_.PROVIDER_ID as PROVIDER7_272_,
capability0_.STATUS as STATUS272_,
capability0_.SUPPORTS_ACCOUNTS as SUPPORTS9_272_,
capability0_.SUPPORTS_CUSTOMER as SUPPORTS10_272_,
capability0_.SUPPORTS_IMAGE as SUPPORTS11_272_,
capability0_.SUPPORTS_STATEMENTS as SUPPORTS12_272_,
capability0_.SUPPORTS_TRANSACTIONS as SUPPORTS13_272_,
capability0_.SUPPORTS_TRANSFER as SUPPORTS14_272_
from
INSTITUTION capability0_
where
capability0_.FINANCIAL_ID=? )
where
rownum <= ?
我已经阅读了这个错误意味着什么,每个派生表都需要一个别名,我可以通过添加别名来编辑派生SQL以使其工作。我的问题是这个。由于这是派生查询(SQL),如何告诉hibernate添加别名,或告诉MySQL不要求它?
Hibernate部分配置:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.showSql" value="true" />
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
答案 0 :(得分:1)
由于ROWNUM
是MySQL不支持的Oracle关键字(例如,请参阅this question),我认为Hibernate在构建查询时仍然以Oracle为目标。
确认您已更改为相应的Hibernate方言。
(为子孙后代添加的答案)
答案 1 :(得分:0)
FROM中的子查询是&#34;派生表&#34;,您必须命名该派生表。
这样的事情应该有效
select
RES.field1, RES.field2
from
( select
capability0_.FINANCIAL_ID as FINANCIAL1_272_,
capability0_.ACTIVE_CONNECTIONS as ACTIVE2_272_,
capability0_.ALLOWED_CONNECTIONS as ALLOWED3_272_,
capability0_.COMPLETE_DETAILS as COMPLETE4_272_,
capability0_.FI_NAME as FI5_272_,
capability0_.MESSAGE_FORMAT as MESSAGE6_272_,
capability0_.PROVIDER_ID as PROVIDER7_272_,
capability0_.STATUS as STATUS272_,
capability0_.SUPPORTS_ACCOUNTS as SUPPORTS9_272_,
capability0_.SUPPORTS_CUSTOMER as SUPPORTS10_272_,
capability0_.SUPPORTS_IMAGE as SUPPORTS11_272_,
capability0_.SUPPORTS_STATEMENTS as SUPPORTS12_272_,
capability0_.SUPPORTS_TRANSACTIONS as SUPPORTS13_272_,
capability0_.SUPPORTS_TRANSFER as SUPPORTS14_272_
from
INSTITUTION capability0_
where
capability0_.FINANCIAL_ID=? ) **RES**
where
rownum <= ?