Java 使用 oracle 和 hql hibernate 函数 to_date

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20140379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:27:13  来源:igfitidea点击:

function to_date with oracle and hql hibernate

javaoraclehibernate

提问by franco

I work with oracle and hql

我与 oracle 和 hql 合作

I have this query :

我有这个查询:

Query query = getSession().createQuery("FROM RequestHealthCertificate  rs  WHERE rs.requestDateHcertif between to_date(:pstartDate,'MM/DD/YYYY') and to_date(:pendDate,'MM/DD/YYYY')");

        //Query query = getSession().createQuery("FROM RequestHealthCertificate  rs  WHERE rs.requestDateHcertif between :pstartDate and :pendDate");

        query.setParameter("pstartDate", startDate);
        query.setParameter("pendDate", endDate);

this is an example of date which will be converted using to_dateThu Nov 07 00:00:00 CET 2013

这是将使用to_date转换的日期示例Thu Nov 07 00:00:00 CET 2013

but when I test I have this error :

但是当我测试时我有这个错误:

ERROR [org.hibernate.util.JDBCExceptionReporter] (http-localhost-127.0.0.1-8080-1) ORA-01858: Caractère non numérique trouvé à la place d'un caractère numérique

09:07:49,635 INFO  [org.directwebremoting.log.accessLog] (http-localhost-127.0.0.1-8080-1) Method execution failed: : org.hibernate.exception.DataException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102) [hibernate3.jar:3.5.3-Final]
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) [hibernate3.jar:3.5.3-Final]

采纳答案by Rafael Andrade

If start and end date are both of type DATE you dont need to convert it. You only need to convert if both values are of type string

如果开始日期和结束日期都是 DATE 类型,则不需要转换它。您只需要在两个值都是字符串类型时进行转换

回答by Linus

You should try this method to set dates than using the to_date.

您应该尝试使用此方法设置日期而不是使用 to_date。

http://www.roseindia.net/hibernate/examples/hql/HQL-date-Between.html

http://www.roseindia.net/hibernate/examples/hql/HQL-date-Between.html

回答by Mitzy Valencia

You have to be sure that those variables are of type String and not DATE, and they have to have the same format.

您必须确保这些变量的类型是 String 而不是 DATE,并且它们必须具有相同的格式。