查询在Oracle DB中工作但不在HQL中工作

时间:2013-06-07 07:54:36

标签: sql oracle hibernate

我正在尝试编写一个简单的查询,我只是从DB中减去两个时间戳。

select ticket_status, 
       count(ticket_status), 
       trunc (to_date('2004/12/14 4:29 PM', 'YYYY/MM/DD HH:MI PM')-to_date('2004/12/12 4:29 PM', 'YYYY/MM/DD HH:MI PM')) as days 
from ITS_TICKET_MASTER 
group by ticket_status, 
trunc (to_date('2004/12/14 4:29 PM', 'YYYY/MM/DD HH:MI PM')-to_date('2004/12/12 4:29 PM', 'YYYY/MM/DD HH:MI PM'))

这个查询给了我想要的结果

但是当我在HQL中尝试相同的查询时,它却给了我错误。

  

QueryImpl(选择ticketStatus,count(ticketStatus),trunc   (to_date('2004/12/14 4:29 PM','YYYY / MM / DD HH:MI   PM') - to_date('2004/12/14 4:29 PM','YYYY / MM / DD HH:MI PM'))   来自WebTicket group by ticketStatus,trunc(to_date('2004/12/14 4:29   PM','YYYY / MM / DD HH:MI PM') - to_date('2004/12/14 4:29 PM','YYYY / MM / DD   HH:MI PM')))Hibernate:选择webticket0_.TICKET_STATUS作为col_0_0_,   将(webticket0_.TICKET_STATUS)计为col_1_0_,   trunc(to_date('2004/12/14 4:29 PM','YYYY / MM / DD HH:MI   PM') - to_date('2004/12/14 4:29 PM','YYYY / MM / DD HH:MI PM'))col_2_0_   来自ITS_TICKET_MASTER webticket0_ group by webticket0_.TICKET_STATUS   ,trunc(to_date('2004/12/14 4:29 PM','YYYY / MM / DD HH:MI   PM') - to_date('2004/12/14 4:29 PM','YYYY / MM / DD HH:MI PM'))   org.hibernate.exception.GenericJDBCException:无法执行查询   在   org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)   在   org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)   在   org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)   在org.hibernate.loader.Loader.doList(Loader.java:1565)at   org.hibernate.loader.Loader.list(Loader.java:1545)at   org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)at at   org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)   在org.hibernate.impl.SessionImpl.list(SessionImpl.java:840)at   org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)at   com.service.FetchFormService.fetchFormDetailsforAgeingReportDayWise(FetchFormService.java:170)   在   com.functionalService.FetchFormFunctionalService.fetchFormDetailsforAgeingReportDayWise(FetchFormFunctionalService.java:275)   在   com.actionClass.AgeingReportAction.fetchAgeingReport(AgeingReportAction.java:49)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)   在   org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:196)   在   org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)   在   org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)   在   org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)   在   org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
  在javax.servlet.http.HttpServlet.service(HttpServlet.java:617)at   

的javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

我对此没有任何答案。请告诉我我做错了什么。

1 个答案:

答案 0 :(得分:0)

(我使用的是带有3.2.6.ga的Oracle驱动程序11.2)

我的查询中的问题似乎是 0 as last_change_date_time 我用sysdate替换它并且它有效。

所以也许你正试图像我一样将数字映射到日期。 在你的*(截断(to_date(' 2004/12/14 4:29 PM',' YYYY / MM / DD HH:MI PM') - to_date(' 2004 / 12/12 4:29 PM',' YYYY / MM / DD HH:MI PM'))为天)*天不应映射到日期

当我这样做时,我遇到了同样的问题:

<hibernate-mapping>
    <class name="myorg.tables.PtProfileData" table="PROFILE_DATA" >
        <composite-id name="id" class="org.openrap.tables.PtProfileDataId">
            <key-many-to-one name="ptVariableDefinition" class="org.openrap.tables.PtVariableDefinition">
                <column name="VARIABLE_ID" length="4" />
            </key-many-to-one>
            <key-property name="dateTime" type="java.util.Date">
                <column name="DATE_TIME" length="7" />
            </key-property>
        </composite-id>
        <property name="valueNumber" type="java.math.BigDecimal">
            <column name="VALUE_NUMBER" precision="22" scale="0">
                <comment>May be add VALUE_TEXT and VALUE_DATE later</comment>
            </column>
        </property>

    <sql-query name="PtProfileData.getprofile">
        <return alias="PROFILE_DATA" class="myorg.tables.PtProfileData"/>
<![CDATA[select variable_id,
trunc(date_time,':timeparam') as date_time,
sum(VALUE_NUMBER) as VALUE_NUMBER ,
**0 as last_change_date_time**
from PROFILE_DATA 
where VARIABLE_ID = :varid and DATE_TIME between :fromtime and :totime 
group by variable_id,trunc(date_time,':timeparam')
order by date_time]]>
    </sql-query>
</hibernate-mapping>