oracle 参数前缀 ':' 后不允许有空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28423741/
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-09-19 02:43:47  来源:igfitidea点击:

Space is not allowed after parameter prefix ':'

javaoraclehibernate

提问by ggDeGreat

My problem is i try to insert text that contain char : in my query

我的问题是我尝试在我的查询中插入包含 char 的文本:

I have tried to put double backslash // before char : but still not working.

我试图在 char 之前加上双反斜杠 // :但仍然无法正常工作。

ABNORMALLY.java.lang.IllegalArgumentException: 
org.hibernate.QueryException: Space is not allowed after parameter prefix ':' 

INSERT INTO TABLE_A  (A_ID, TYPE_ID, F_ID, REFNO, RECORD) VALUES 
( A_ID_SEQ.nextval, 4 , 9 , 'NY167', q'[LA2010167|SNIP' N CLIP|LMG|1.Unit no\: 1046, 1 st Floor, Limbang Plaza, 98700 Limbang|2010-12-10||]')

采纳答案by Nathan Hughes

Here Hibernate is parsing an insert that contains a hard-coded value that has a colon in it. If you rewrite the insert to use parameters then Hibernate won't see the value as part of the statement.

这里 Hibernate 正在解析一个插入,其中包含一个硬编码的值,其中有一个冒号。如果您重写插入以使用参数,则 Hibernate 不会将该值视为语句的一部分。

回答by Arjun Nagathankandy

From my experience I will tell you. There are two scenarios
1) You want to specify a parameter in the query whose value set dynamically.

以我的经验告诉你。有两种情况
1) 您想在查询中指定一个参数,其值是动态设置的。

eg: where user_id = :userId

Here you wont get any problem if you are setting parameter with same name as "userId";
2) You are typecasting the value

如果您设置与“userId”同名的参数,在这里您不会遇到任何问题;
2)您正在对值进行类型转换

eg: select count(id) :: integer

when you are doing this you have to use escape character otherwise hibernate will think that it is a parameter. And it will give an error "All parameters are not set "you can overcome this with writing code using escape character

当你这样做时,你必须使用转义字符,否则休眠会认为它是一个参数。它会给出一个错误“所有参数都没有设置”你可以通过使用转义字符编写代码来克服这个问题

eg:select count(id) \:\: integer

So this will solve your problem. And if you are wrongly used forward slash instead of backward slash you will get the error "space is not allowed after prefix"

所以这将解决你的问题。如果您错误地使用正斜杠而不是反斜杠,您将收到错误“前缀后不允许空格”

Wrong: select count(id)//://: integer
Right: select count(id)\:\: integer

But I highly recommended you to use the CAST function instead of using "::"this operator ie select CAST(count(id) as integer)It is the better way of type casting and it will lead to minimal errors

但我强烈建议你使用 CAST 函数而不是使用"::"这个运算符,即select CAST(count(id) as integer)它是更好的类型转换方法,它会导致最小的错误

回答by Mohamed K.

The problem is that your RECORD column contains ":", so, Hibernate waits for a parameter after it. I hade the same problem of you

问题是您的 RECORD 列包含“:”,因此,Hibernate 会等待其后的参数。我和你有同样的问题