尝试执行使用空参数值的 native SQL 查询时出现 Oracle 错误。我把它归结为这个测试用例:
String SQL_SELECT =
"select * from act t1 where t1.G074=:G074 AND t1.G542=:G542 AND
nvl(t1.svhlick,' ')=nvl(:svhlick,' ')
and t1.svhlic=:svhlic
and t1.sf$version =
(select max(sf$version) from act t2 where t2.G074=t1.G074 and t2.G542=t1.G542 AND nvl
(t2.svhlick,' ')=nvl(t1.svhlick,' ') and t2.svhlic=t1.svhlic)";
Query q = em.createNativeQuery(SQL_SELECT, Act.class);
….
q.setParameter("svhlick", entity.getSvhlick());
…
the query uses Oracle's NVL
function to handle null
parameters.
However, when I run this code, it fails with an error on the q.getResultList ()
call.
The error is as follows:
ORA-01465: invalid hex number
Now, i'm resolved this problem as
q.setParameter("svhlick", (entity.getSvhlick() == null ? " " : entity.getSvhlick()));
但是这个解决方案并不好。
数据库 Oracle 11gR2 win1251 编码。
Netbeans 7.3.1 项目编码 UTF-8。
entity.getSvhlick() - 字符串 Oracle 中的 svhlick - varchar2(1)
我认为问题可能是由于不同的编码。
有什么帮助吗?
最佳答案
我遇到了同样的问题并通过指定类型解决了它:
q.setParameter("svhlick", entity.getSvhlick(), StandardBasicTypes.STRING);
关于 hibernate + nvl()= 甲骨文 01465,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18915598/