[英]How can I use MySQL assign operator(:=) in hibernate native query?
I'm using Hibernate.我正在使用休眠。 I wrote some native query because I need to use sub select statement.
我写了一些本机查询,因为我需要使用 sub select 语句。
Query looks like this:查询如下所示:
SELECT sub.rownum FROM
(SELECT k.`news_master_id` AS id, @row := @row + 1 AS rownum
FROM keyword_news_list k
JOIN (SELECT @row := 0) r
WHERE k.`keyword_news_id` = :kid
ORDER BY k.`news_master_id` ASC) AS sub
WHERE sub.id = :nid
When I run this query like this:当我像这样运行这个查询时:
sessionFactory.getCurrentSession()
.createSQLQuery(query)
.setParameter("kid", kid)
.setParameter("nid", nid)
.uniqueResult();
This exception comes out:这个异常出来了:
org.hibernate.QueryException: Space is not allowed after parameter prefix ':' ....
This might because of :=
operator.这可能是因为
:=
运算符。 I found some Hibernate issue about this.我发现了一些关于此的Hibernate 问题。 This issue is still open.
这个问题仍然是开放的。 Isn't there any solution for this problem?
这个问题没有解决办法吗?
Note that HHH-2697 is now fixed for Hibernate 4.1.3 You can now escape with backslash:请注意, HHH-2697现在已针对 Hibernate 4.1.3 修复,您现在可以使用反斜杠转义:
SELECT k.`news_master_id` AS id, @row \:= @row + 1 AS rownum
FROM keyword_news_list k
JOIN (SELECT @row \:= 0) r
WHERE k.`keyword_news_id` = :kid
ORDER BY k.`news_master_id` ASC
Another solution for those of us who can't make the jump to Hibernate 4.1.3.对于我们这些无法跳转到 Hibernate 4.1.3 的人来说,这是另一个解决方案。
Simply use /*'*/:=/*'*/
inside the query.只需在查询中使用
/*'*/:=/*'*/
。 Hibernate code treats everything between '
as a string (ignores it). Hibernate 代码将
'
之间'
所有内容视为字符串(忽略它)。 MySQL on the other hand will ignore everything inside a blockquote and will evaluate the whole expression to an assignement operator.另一方面,MySQL 将忽略块引用内的所有内容,并将整个表达式计算为赋值运算符。
I know it's quick and dirty, but it get's the job done without stored procedures, interceptors etc.我知道它又快又脏,但它可以在没有存储过程、拦截器等的情况下完成工作。
you can implement this is a slightly different way.. you need to replace the : operator with something else (say '|' char ) and in your interceptor replace the '|'你可以实现这是一种稍微不同的方式..你需要用其他东西替换 : 运算符(比如 '|' char )并在你的拦截器中替换 '|' with the : .
与:。
this way hibernate will not try to think the : is a param but will ignore it这样 hibernate 不会试图认为 : 是一个参数,但会忽略它
For the interceptor logic you can refer to the hibernate manual拦截器逻辑可以参考hibernate手册
This has worked for me using MySQL 5.这对我使用 MySQL 5 有用。
remember, this replacing of : must be only done to ':=' and other MySQL specific requirments.. don't try to replace the : for the param-placeholders.请记住,这种替换 : 必须仅针对 ':=' 和其他 MySQL 特定要求进行。不要尝试替换 : 的参数占位符。 (hibernate will not be able to identify the params then)
(此时休眠将无法识别参数)
我更喜欢包含 Spring JDBC 并执行查询,而不是与 Hibernate 拦截器作斗争。
在遇到 mysql时的Hibernate 异常中:= operator Stanislav提供了除拦截器之外的另一个选项来解决此问题
If you keep your SQL-files away from Java code - try this piece of code.如果您让 SQL 文件远离 Java 代码 - 试试这段代码。 Played a lot to get the right number of escaping slashes:
玩了很多以获得正确数量的转义斜杠:
String sqlPattern = FileUtils.readFile(this.getClass(), /sql/my_query.sql");
sqlPattern = sqlPattern.replaceAll(":=", "\\\\:=");
Query query = entityManager.createNativeQuery(sqlPattern);
我想在 = 之后不应该有空格,运算符应该写为 =:(没有任何空格)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.