oracle ORA-00932: 不一致的数据类型:Hibernate 中的预期 DATE 为 BINARY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16582611/
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
ORA-00932: inconsistent datatypes: expected DATE got BINARY in Hibernate
提问by BaN3
My query is like this:
我的查询是这样的:
where (:startDate is null or :endDate is null or DDATE between :startDate AND :endDate)
AND (:startDate is null or (:endDate is not null or DDATE between :startDate AND :date))
I get startDate
and endDate
from ajax date picker. date
is the system date, which I am getting like this:
我从 ajax 日期选择器获取startDate
和获取endDate
。date
是系统日期,我得到的是这样的:
Date utiDate = new Date();
When I execute my query, I get the error:
当我执行查询时,出现错误:
java.sql.SQLException: ORA-00932: inconsistent datatypes: expected DATE got BINARY
采纳答案by eternay
Trying to find the correct answer, I found an interesting post here.
试图找到正确的答案,我在这里找到了一个有趣的帖子。
If :endDate
is null, you can't be sure the condition DDATE between :startDate AND :endDate
won't be evaluated. And if it's evaluated, Oracle will try to convert a null value to a date, so it'll give you an error.
如果:endDate
为空,则不能确定DDATE between :startDate AND :endDate
不会评估条件。如果它被评估,Oracle 会尝试将一个空值转换为一个日期,所以它会给你一个错误。
Try to test the query removing the DDATE between :startDate AND :endDate
part: you shouldn't have the error anymore. Then you'll have to modify your query to be sure the between
operator won't be evaluated if :enddate
is null. In this post, they recommend using CASE
statements inside the WHERE
clause. Maybe it can solve your problem.
尝试测试删除DDATE between :startDate AND :endDate
部分的查询:您不应该再有错误了。然后你必须修改你的查询以确保between
如果:enddate
为 null则不会评估运算符。在这篇文章中,他们建议CASE
在WHERE
子句中使用语句。也许它可以解决你的问题。
About short-circuit evaluation and Oracle database, I found that questionthat can help you understand the problem.
关于短路计算和Oracle数据库,我发现这个问题,可以帮助你理解这个问题。
回答by radzimir
Properties :startDate and :endDate in your bean are not of type java.util.Date, but something else that Hibernate can't convert to SQL-DATE automatically and is sending BINARY as default.
bean 中的属性 :startDate 和 :endDate 不是 java.util.Date 类型,而是 Hibernate 无法自动转换为 SQL-DATE 并且默认发送 BINARY 的其他属性。
You need to implement javax.persistence.AttributeConverter< X, Y >
你需要实现javax.persistence.AttributeConverter< X, Y >
There is some tutorialon that.
有一些教程。
Hint: if you place that converter in some jar included in your war app, you must explicitly list it in persistemce.xml, otherwise your converter will not be found:
提示:如果您将该转换器放置在包含在您的 war 应用程序中的某个 jar 中,则必须在 persistemce.xml 中明确列出它,否则将找不到您的转换器:
<persistence-unit name="default" transaction-type="JTA">
<jta-data-source>java:/mydb</jta-data-source>
<!-- register converter -->
<class>com.example.persistence.LocalDateTimeConverter</class>
...