How can I use MySQL assign operator(:=) in hibernate native query?

前端 未结 6 1439
情歌与酒
情歌与酒 2020-11-28 13:13

I\'m using Hibernate. I wrote some native query because I need to use sub select statement.

Query looks like this:

SELECT sub.rownum FROM 
    (SELEC         


        
相关标签:
6条回答
  • 2020-11-28 14:01

    in Hibernate exception on encountering mysql := operator Stanislav gave another option other than interceptor to solve this issue

    0 讨论(0)
  • 2020-11-28 14:02

    I prefer to include Spring JDBC and execute the query rather than fight against Hibernate interceptors.

    0 讨论(0)
  • 2020-11-28 14:05

    I guess there should not be a space after = , the operator should be written as =: (without any spaces)

    0 讨论(0)
  • 2020-11-28 14:10

    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 '|' with the : .

    this way hibernate will not try to think the : is a param but will ignore it

    For the interceptor logic you can refer to the hibernate manual

    This has worked for me using 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. (hibernate will not be able to identify the params then)

    0 讨论(0)
  • 2020-11-28 14:14

    Note that HHH-2697 is now fixed for Hibernate 4.1.3 You can now escape with backslash:

    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
    
    0 讨论(0)
  • 2020-11-28 14:19

    Another solution for those of us who can't make the jump to Hibernate 4.1.3.
    Simply use /*'*/:=/*'*/ inside the query. Hibernate code treats everything between ' as a string (ignores it). MySQL on the other hand will ignore everything inside a blockquote and will evaluate the whole expression to an assignement operator.
    I know it's quick and dirty, but it get's the job done without stored procedures, interceptors etc.

    0 讨论(0)
提交回复
热议问题