简体   繁体   中英

Switched from Oracle Db to MySQL. Named query no longer works

This works with Oracle, but we have been told to switch to MySQL. After changing the driver info and other pertinent settings to point to a MySQL DB, I am no longer able to execute my named queries. Here is the simplest one as defined in the Entity:

@NamedQuery(name="get_capability", query="select cb from Capability cb where cb.financial_Id = :fiId")

Then I have code to execute the query as follows:

query = em.createNamedQuery("get_capability");

. . .

query.setParameter("fiId", fiId);

query.setMaxResults(1);

cabability = (Capability) query.getSingleResult();

This last line throws an exception:

2013-04-24 10:46:00,677 WARN [org.hibernate.util.JDBCExceptionReporter] (http-127.0.0.1-8080-1) SQL Error: 1248, SQLState: 42000

2013-04-24 10:46:00,677 ERROR [org.hibernate.util.JDBCExceptionReporter] (http-127.0.0.1-8080-1) Every derived table must have its own alias

The SQL that gets generated is logged in the log file as follows:

/* 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 <= ?

I've read what this error means, that there needs to be an alias for every derived table, and I can edit the derived SQL to get it to work by adding an alias. My question is this. Since this is a derived query (SQL), how do I tell hibernate to add an alias, or tell MySQL to not require it?

Hibernate partial config:

<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"/>

Since ROWNUM is a Oracle keyword that is not supported in MySQL (for example, see this question ) I think Hibernate is still targetting Oracle when it builds the queries.

Verify that you've changed to the corresponding Hibernate dialect.

(Answer added for posterity)

your subquery in the FROM is the "derived table", you have to name that derived table.

Something like this should work

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 <= ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM