SQL异常:查询中没有此类列时找不到列

时间:2018-04-27 10:35:53

标签: mysql hibernate hibernate-entitymanager

我正在尝试在我的java应用程序中运行一个查询,但它会抛出一个异常,我将很快粘贴堆栈跟踪。当我在mysql终端上运行相同的查询时,它运行完美。但是当javax持久性出现时,它会爆炸。

Query query = entityManager.createNativeQuery(myQuery);
List<String> resultList = query.getResultList();

这是我得到的例外。

java.sql.SQLException: Column 'id' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2813)
    at org.hibernate.type.IntegerType.get(IntegerType.java:28)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
    at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:474)
    at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:420)
    at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:317)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606)
    at org.hibernate.loader.Loader.doQuery(Loader.java:701)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2213)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:65)

以下是查询:

select uqd.id as requestId,count(*) as dailyRunCount,sum(finalTab.diffInMinutes) as totalRunningTimeInMinutes,config_level,user_email_id,team_name, description,cast(sum(finalTab.diffInMinutes) * 3.35 as decimal(10,2)) as cost  from user_query_details uqd join (select a.request_id,(b.state_change_time_stamp - a.state_change_time_stamp)/60000 as diffInMinutes from (select request_id,job_id,state_change_time_stamp from user_request_history where state = 'RUNNING' and state_change_time_stamp >= (UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)*1000)) a join (select job_id,state_change_time_stamp from user_request_history where state ='COMPLETED') b on a.job_id = b.job_id) finalTab on uqd.id=finalTab.request_id where uqd.discriminator='S' and query is not null and config_level='LEVEL_TWO' group by uqd.id order by totalRunningTimeInMinutes desc limit 3

任何帮助都会受到很大的影响。感谢。

1 个答案:

答案 0 :(得分:0)

更改了查询

SELECT 
      uqd.id as requestID,count(*) as dailyRunCount,
      sum(finalTab.diffInMinutes) as totalRunningTimeInMinutes,
      config_level,user_email_id,team_name, 
      description,
      cast(sum(finalTab.diffInMinutes) * 3.35 as decimal(10,2)) as cost  
    FROM 
      user_query_details uqd join 
       (SELECT 
         a.request_id,
        (b.state_change_time_stamp - a.state_change_time_stamp)/60000 as diffInMinutes 
        FROM 
         (SELECT 
             request_id,job_id,state_change_time_stamp 
          FROM 
             user_request_history 
          WHERE 
            state = 'RUNNING' and 
            state_change_time_stamp >= (UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)*1000)) a join 
(select job_id,state_change_time_stamp from user_request_history where state ='COMPLETED') b on a.job_id = b.job_id) finalTab on uqd.id=finalTab.request_id where uqd.discriminator='S' and query is not null and config_level='LEVEL_TWO' group by requestID order by totalRunningTimeInMinutes desc limit 3