-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to use MySQL variables in Hibernate?
PostPosted: Tue Nov 15, 2016 5:23 am 
Newbie

Joined: Mon Nov 14, 2016 4:16 pm
Posts: 5
SET @number_of_rows := (SELECT COUNT(*) FROM carecube.visit);
SET @quartile := (ROUND(@number_of_rows*0.25));
SET @medianquartile := (ROUND(@number_of_rows*0.50));
SET @sql_q1 := (CONCAT('(SELECT "Q1" AS quartile, visit.id FROM carecube.visit order by visit.id LIMIT 1 OFFSET ', @quartile, ')'));
SET @sql_med := (CONCAT('(SELECT "Median" AS quartile, visit.id FROM carecube.visit order by visit.id LIMIT 1 OFFSET ', @medianquartile, ')'));
SET @sql_max := (CONCAT('SELECT "MAX" AS quartile, max(visit.id) as id FROM carecube.visit order by visit.id '));
SET @sql_q3 := (CONCAT('(SELECT "Q3" AS quartile, visit.id FROM carecube.visit order by visit.id DESC LIMIT 1 OFFSET ', @quartile, ');'));
SET @sql_min := (CONCAT('SELECT "MIN" AS quartile, min(visit.id) as id FROM carecube.visit'));

SET @sql := (CONCAT_WS(' UNION ', @sql_min, @sql_q1, @sql_med, @sql_q3));

PREPARE stmt1 from @sql;
EXECUTE stmt1;

This executes perfectly in MySQL workbench, but complains of syntax errors near prepare/execute in hibernate in java.

Can someone with more knowledge of using sql variables shed some light on this please? thank you.

Thanks.


Top
 Profile  
 
 Post subject: Re: How to run MySQL variables in hibernate?
PostPosted: Tue Nov 15, 2016 6:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You need to extract this script into a stored procedure and then you need to call it from Hibernate.

Check out this article for more on calling stored procedures and functions from Hibernate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.