-->
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.  [ 4 posts ] 
Author Message
 Post subject: Running a MySQL native query which uses user-defined
PostPosted: Mon Nov 14, 2016 5:14 pm 
Newbie

Joined: Mon Nov 14, 2016 4:16 pm
Posts: 5
I have a Java application which runs MySQL queries in Hibernate.

I am now trying to execute the following MySQL query in hibernate which uses user-defined variables.
I have overcome the issue surrounding ':=' assignments, by adding two backslashes. The compiler is no longer complaining about this, but when I try to execute this query from within my application, it displays the following error message:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PREPARE stmt1 from @sql_max; EXECUTE stmt1' at line 1"

My query in Java/hibernate is as follows:
Code:
    private static final String COUNT_INTERQUARTILE_SQL
=  " SET @sql_max \\:=('SELECT ''Max'' AS quartile, max(visit.id) as id " +
   " FROM carecube.visit order by visit.id ');" +
   " PREPARE stmt1 from @sql_max;" +
   " EXECUTE stmt1;";


Could an experienced Hibernate and/or MySQL expert spot my syntax issues? Thank you all for your help!


Top
 Profile  
 
 Post subject: Re: Running a MySQL native query which uses user-defined
PostPosted: Tue Nov 15, 2016 1:16 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You don't need to prepare/execute the statement manually because that's the job of a JDBC PreparedStatement, which Hibernate uses behind the scenes.

This is how you should run your SQL query:

Code:
List<Object[]> result = session.createSQLQuery(
" SELECT 'Max' AS quartile, max(visit.id) as id " +
" FROM carecube.visit order by visit.id")
.list();


Top
 Profile  
 
 Post subject: Re: Running a MySQL native query which uses user-defined
PostPosted: Tue Nov 15, 2016 4:39 am 
Newbie

Joined: Mon Nov 14, 2016 4:16 pm
Posts: 5
Thanks for your reply.

I am referencing that query by calling it within this function which looks similar to yours.

Code:
public List<ProcedureDuration> interQuartileRange() {
      // TODO Auto-generated method stub
      return this.template.execute(new HibernateCallback<List<ProcedureDuration>>() {

         @SuppressWarnings("unchecked")
         @Override
         public List<ProcedureDuration> doInHibernate(Session session) throws HibernateException {
            Query query = session.createSQLQuery(COUNT_INTERQUARTILE_SQL);
            //return InterQuartileRange.newInstance(query.list());
            //return (List<TotalCaseNumbers>)query.list();
            return (List<ProcedureDuration>)query.list();
         }
      });
   }


Can Hibernate read user-defined variables?

Thanks.


Top
 Profile  
 
 Post subject: Re: Running a MySQL native query which uses user-defined
PostPosted: Tue Nov 15, 2016 6:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
No, it can not.

You can call stored procedures or database functions, as explained in this article, but you cannot extract user-defined variables like you want, and you don't even need to do so. You can use the ResultSet instead.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.