-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to pass values to Named Query dynamically in Hibernate
PostPosted: Thu Feb 11, 2010 12:57 am 
Newbie

Joined: Wed Feb 10, 2010 7:19 pm
Posts: 12
I need to create a dynamic query based on the some condition. I don't want to hardcode this query in my java class.
So that's the reason i want to get this using Named query in hibernate.
Below is how iam trying to implement.

DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String today = sdf.format(date);
Query query = session.getNamedQuery("findDateDifference");
query.setString(1, today);
List date_Diff_List = query.list();

<sql-query name="findDateDifference"><![CDATA[
select (? - temp.bv_date) as days from
( select CLO_DTE as bv_date from BRAND ) temp]]>
</sql-query>

I need to pass the value to the '?' which is given in above query.
When I run the above code it gives me error as below.

Can anyone please give me some suggestions how to fix this issue.


Hibernate:
select (? - temp.bv_date) as days from
( select CLO_DTE as bv_date from BRAND ) temp

org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:1502)
at org.hibernate.loader.Loader.list(Loader.java:1482)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:103)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1333)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:146)
at com.hibernate.ExceptionTest.getDateDifference(ExceptionTest.java:219)
at com.hibernate.ExceptionTest.main(ExceptionTest.java:43)
Caused by: com.ibm.db2.jcc.a.SqlException: DB2 SQL error: SQLCODE: -171, SQLSTATE: 42815, SQLERRMC: 2;-
at com.ibm.db2.jcc.a.hd.e(hd.java:1659)
at com.ibm.db2.jcc.a.hd.a(hd.java:1235)
at com.ibm.db2.jcc.a.hd.a(hd.java:1221)
at com.ibm.db2.jcc.c.jb.h(jb.java:142)
at com.ibm.db2.jcc.c.jb.a(jb.java:43)
at com.ibm.db2.jcc.c.w.a(w.java:30)
at com.ibm.db2.jcc.c.cc.g(cc.java:160)
at com.ibm.db2.jcc.a.hd.n(hd.java:1215)
at com.ibm.db2.jcc.a.id.gb(id.java:1780)
at com.ibm.db2.jcc.a.id.d(id.java:2255)
at com.ibm.db2.jcc.a.id.X(id.java:505)
at com.ibm.db2.jcc.a.id.executeQuery(id.java:488)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:107)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1183)
at org.hibernate.loader.Loader.doQuery(Loader.java:363)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:203)
at org.hibernate.loader.Loader.doList(Loader.java:1499)
... 6 more


Top
 Profile  
 
 Post subject: Re: How to pass values to Named Query dynamically in Hibernate
PostPosted: Fri Feb 12, 2010 1:20 pm 
Newbie

Joined: Wed Feb 10, 2010 7:19 pm
Posts: 12
Hi,
Can anyone please responed to this issue. Its urgent.
Thanks in advance.


Top
 Profile  
 
 Post subject: Re: How to pass values to Named Query dynamically in Hibernate
PostPosted: Fri Feb 12, 2010 2:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Dunno, what is "Caused by: com.ibm.db2.jcc.a.SqlException: DB2 SQL error: SQLCODE: -171, SQLSTATE: 42815, SQLERRMC: 2;-"?


Top
 Profile  
 
 Post subject: Re: How to pass values to Named Query dynamically in Hibernate
PostPosted: Fri Feb 12, 2010 7:08 pm 
Newbie

Joined: Wed Feb 10, 2010 7:19 pm
Posts: 12
Hibernate is trying to bind the values where do you have where condition in your SQL.
For example
select emp_id,empname,status,department from emp where emp_id = ?

Query query = session.getNamedQuery("findEmp");
query.setInteger(1, 12);
List results = query.list();

As per the above condition it work perfect. But when iam trying to bind the values in some other place like the below, it gives me the problem

Query query = session.getNamedQuery("findDateDifference");
query.setString(1, today);
List date_Diff_List = query.list();

<sql-query name="findDateDifference"><![CDATA[
select (? - temp.bv_date) as days from
( select CLO_DTE as bv_date from BRAND ) temp]]>
</sql-query>


Top
 Profile  
 
 Post subject: Re: How to pass values to Named Query dynamically in Hibernate
PostPosted: Sat Feb 13, 2010 11:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
you are still not telling us what that db2 error means. I am not a db2 user nor am I going to google/doc it for you. After we know what the db2 error means we can probably help you.

I am actually surprised the query parses at all. Db2 is very touchy about parameters in the select clause


Top
 Profile  
 
 Post subject: Re: How to pass values to Named Query dynamically in Hibernate
PostPosted: Mon Feb 15, 2010 12:10 am 
Newbie

Joined: Wed Feb 10, 2010 7:19 pm
Posts: 12
Iam not sure what does that error mean.
I think it is not able to parse the SQL query which is generated.
Any idea


Top
 Profile  
 
 Post subject: Re: How to pass values to Named Query dynamically in Hibernate
PostPosted: Mon Feb 15, 2010 3:12 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
http://www.sqlerror.de/db2_sql_error_-1 ... 42815.html

I think you'd better post in a DB2 forum to get help writing a correct SQL statement.


Top
 Profile  
 
 Post subject: Re: How to pass values to Named Query dynamically in Hibernate
PostPosted: Mon Feb 15, 2010 11:29 am 
Newbie

Joined: Wed Feb 10, 2010 7:19 pm
Posts: 12
Thanks for the reply.

But when i tried to execute this query on DB2 it works fine. It doesn't give me any problem.
I tried as following

select (Current Date - temp.bv_date) as days from
( select CLO_DTE as bv_date from BRAND ) temp


select ('2010-02-15' - temp.bv_date) as days from
( select CLO_DTE as bv_date from BRAND ) temp

Both of these queries works fine.

So i believe it is not the problem with DB2. This should be something which Hibernate is not trying to recognize the query.

But same query when executed with

Query date_diff_Query = session.createSQLQuery("select (? - temp.bv_date) as days from
( select CLO_DTE as bv_date from BRAND ) temp" );
date_diff_Query.setString(0, today);

It works perfect. So its only problem with the Named Query.

Thanks


Top
 Profile  
 
 Post subject: Re: How to pass values to Named Query dynamically in Hibernate
PostPosted: Wed Feb 17, 2010 4:19 pm 
Newbie

Joined: Wed Feb 10, 2010 7:19 pm
Posts: 12
Any idea on this...
Please reply.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.