2014-04-09 72 views
0
List employees = session.createQuery("FROM Employee where date1 between '04-APR-04 02.27.05.187000 AM' and '04-APR-14 02.27.05.187000 AM' and YEAR('04-APR-04 02.27.05.187000 AM')=YEAR('04-APR-04 02.27.05.187000 AM')").list(); 

我得到這個問題:如何獲得一年日期HQL甲骨文

Hibernate: select emThrployee0_.id as id2_, employee0_.firstName as firstName2_, employee0_.lastName as lastName2_, employee0_.date1 as date4_2_ from emp1 employee0_ where (employee0_.date1 between '04-APR-04 02.27.05.187000 AM' and '04-APR-14 02.27.05.187000 AM') and extract(year from '04-APR-04 02.27.05.187000 AM')=extract(year from '04-APR-04 02.27.05.187000 AM') 
org.hibernate.exception.GenericJDBCException: could not execute query 
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 
    at org.hibernate.loader.Loader.doList(Loader.java:2214) 
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095) 
    at org.hibernate.loader.Loader.list(Loader.java:2090) 
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:388) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338) 
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172) 
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121) 
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79) 
    at main.StoreData.listEmployees(StoreData.java:68) 
    at main.StoreData.main(StoreData.java:57) 
Caused by: java.sql.SQLException: ORA-30076: invalid extract field for extract source 

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) 
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331) 
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288) 
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743) 
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216) 
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:799) 
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1038) 
    at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:839) 
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1133) 
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285) 
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3329) 
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186) 
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1778) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:662) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) 
    at org.hibernate.loader.Loader.doList(Loader.java:2211) 
    ... 9 more 

我需要一年時間處於休眠甲骨文 但在SQL Server中工作的代碼,但在甲骨文不工作。 我嘗試,但我解決不了請如果知道

感謝

回答

0

隱式轉換任何幫助都沒有,因爲他們是在SQL Server中的Oracle accomidating。查看將日期時間字符串填充到查詢中的TO_DATE()函數中。

http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm

+0

我需要Oracle和SQL服務器的通用HQL代碼。 – sree

+0

我收到Ans:FROM Employee where'date1'在'2014-04-04 08:58:45.000'和'2014-04-14 08:58:45.000'並且摘錄(從date1開始)= to_char(to_timestamp('2014- 04-10 08:58:45.000','yyyy-mm-rr hh:mi:ss.ff am'),'yyyy') – sree

0

我會嘗試閱讀文檔,因爲您的查詢的結構應是這樣的:

Select * FROM table t WHERE t.column... 

您可以嘗試類似如下:

"SELECT * FROM Employee e WHERE e.date1 > '2013-01-01' AND e.date1 <'2014-01-01'" 

這將讓所有你的員工表中的列可以用these格式進行實驗。使用這些格式,您可以使用功能。

+0

year(date)query is working in sql server.what is alternative alternative for Oracle in HQL。 – sree

+0

它是Hibernate的文檔的一部分,以及你的日期只是格式錯誤。 –