简体   繁体   English

如何从HQL for Oracle中获取日期开始的年份

[英]how to get year from date in HQL for Oracle

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

I am getting this Issue: 我收到此问题:

    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

i need year from date in hibernate for Oracle but this code working in Sql server but in Oracle is not Working. 对于Oracle,我需要从休眠状态开始,但是该代码在Sql Server中有效,但在Oracle中不起作用。 i try but i can't solve pls help any if knows 我尝试但我无法解决请帮助,如果知道

Thanks 谢谢

Implicit conversions aren't as accomidating in Oracle as they are in SQL Server. Oracle中的隐式转换不像SQL Server中那样隐式转换。 Look at stuffing your date time strings into the TO_DATE() function in your query. 查看将日期时间字符串填充到查询中的TO_DATE()函数中。

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

I would try reading the documentation because your query should be structured this way: 我会尝试阅读文档,因为您的查询应采用以下结构:

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

You can try something like the following: 您可以尝试以下操作:

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

This will get all columns from your employee table and you can experiment with dates in these formats. 这将从您的员工表中获取所有列,您可以尝试使用这些格式的日期。 Using those formats you can use the function YEAR(date) . 使用这些格式,您可以使用函数YEAR(date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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