如何在HQL for Oracle中获取年份

时间:2014-04-09 13:38:25

标签: sql sql-server oracle hibernate hql

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();

我收到了这个问题:

    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

我需要从hibernate for Oracle开始 但是这个代码在Sql server中运行但在Oracle中不起作用。 我尝试,但我无法解决,如果知道

,请帮忙

谢谢

2 个答案:

答案 0 :(得分:0)

隐式转换在Oracle中并不像在SQL Server中那样适应。查看将日期时间字符串填入查询中的TO_DATE()函数。

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

答案 1 :(得分: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格式的日期。使用这些格式,您可以使用函数YEAR(date)