[hibernate-issues] [Hibernate-JIRA] Created: (HHH-4914) How to pass values to Named Query dynamically in Hibernate

kalyan buddala (JIRA) noreply at atlassian.com
Fri Feb 12 18:00:29 EST 2010


How to pass values to Named Query dynamically in Hibernate
----------------------------------------------------------

                 Key: HHH-4914
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4914
             Project: Hibernate Core
          Issue Type: Bug
    Affects Versions: 3.1
            Reporter: kalyan buddala


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



-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list