Ommadawn
Ommadawn

Reputation: 2730

Hibernate escaping ":" character

I've found some posts here and some answers. I've tried every each of them with bad results. I don't want to use stored procedures.

I'm using Hibernate in Java, with MySQL. Some of my query string is:

SET @rank = 0; 
SET @limit = 0.1; 
SELECT (@limit := ROUND(COUNT(id) * @limit)) FROM Pics;
SELECT Q1.popularity FROM Pics AS p INNER JOIN (
   SELECT IF((@rank := @rank + 1) <= @limit, true, false) AS 'popularity', p.id 
   FROM Pics AS p 
   ORDER BY p.points DESC
) AS Q1 ON Q1.id = p.id WHERE p.id = 7;

Some people said a solution is using /*'*/:=/*'*/ inside the query (post), but it doesn't work for me:

SET @rank = 0; 
SET @limit = 0.1; 
SELECT (@limit /*'*/:=/*'*/ ROUND(COUNT(id) * @limit)) FROM Pics;
SELECT Q1.popularity FROM Pics AS p INNER JOIN (
   SELECT IF((@rank /*'*/:=/*'*/ @rank + 1) <= @limit, true, false) AS 'popularity', p.id 
   FROM Pics AS p 
   ORDER BY p.points DESC
) AS Q1 ON Q1.id = p.id WHERE p.id = 7;

I'm despereted!

Any idea, please? I'd be so grateful!

Thanks!

Upvotes: 1

Views: 1031

Answers (1)

Ommadawn
Ommadawn

Reputation: 2730

Finally I could fix it using double backslash inside the Java query code:

SET @rank = 0; 
SET @limit = 0.1; 
SELECT (@limit \\:= ROUND(COUNT(id) * @limit)) FROM Pics;
SELECT Q1.popularity FROM Pics AS p INNER JOIN (
   SELECT IF((@rank \\:= @rank + 1) <= @limit, true, false) AS 'popularity', p.id 
   FROM Pics AS p 
   ORDER BY p.points DESC
) AS Q1 ON Q1.id = p.id WHERE p.id = 7;

Upvotes: 1

Related Questions