-->
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: java.sql.Date Required in Native Query
PostPosted: Thu Nov 12, 2009 1:17 pm 
Newbie

Joined: Thu Nov 12, 2009 1:03 pm
Posts: 1
I am doing a native SQL query in JPA, but I get an error if I try to set a java.util.Date as a parameter.

Code:
[JDBCExceptionReporter] The value of input host variable or parameter number "1" cannot be used because of its data type.


The following code demonstrates the problem:

Code:
@PersistenceContext
HibernateEntityManager em;

String sql="SELECT * FROM TABLE1 WHERE DATE_CHANGED >= :changeDate";

public List getRecordsChanged() {
      Date date = DateUtils.addMonths(new Date(), -1);
      return em.createNativeQuery(sql, Table1.class)
         .setParameter("changeDate", date)
         .getResultList();
}


It works if I change it to:

Code:
@PersistenceContext
HibernateEntityManager em;

String sql="SELECT * FROM TABLE1 WHERE DATE_CHANGED >= :changeDate";

public List getRecordsChanged() {
      Date date = DateUtils.addMonths(new Date(), -1);
      return em.createNativeQuery(sql, Table1.class)
         .setParameter("changeDate", new java.sql.Date(date.getTime()))
         .getResultList();
}


Is there a property that I can set so that Hibernate will do this conversion automatically?

EDIT: Nevermind. Should have been:

Code:
@PersistenceContext
HibernateEntityManager em;

String sql="SELECT * FROM TABLE1 WHERE DATE_CHANGED >= :changeDate";

public List getRecordsChanged() {
      Date date = DateUtils.addMonths(new Date(), -1);
      return em.createNativeQuery(sql, Table1.class)
         .setParameter("changeDate", date, TemporalType.DATE)
         .getResultList();
}


Top
 Profile  
 
 Post subject: Re: java.sql.Date Required in Native Query
PostPosted: Fri Jan 31, 2014 4:31 pm 
Newbie

Joined: Fri Mar 19, 2010 2:37 pm
Posts: 5
Hi andrew,
To solve this issue you have to set actual type parameter.

If you want to Compare only Date (e.g. "YYYY-MM-DD" ) then use > query.setDate("parameterName", datevalue);
If you want to compare Time only (e.g. hh:mm24:ss ) then use > query.setTime("parameterName", datevalue);

If you wanted to compare Date and Time (e.g. "YYYY-MM-DD hh:mm24:ss" ) then use > query.setTimestamp("parameterName", datevalue);

It is better not to use new java.sql.Date(date.getTime()))

But using TemporalType is also the right way too.

Regards,
Venod

_________________
Venod Raveendran
Architect-Technology
[color=#BF4040]Congizant Technology Solutions[/color]


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.