users@glassfish.java.net

problem with JPA query for date handling

From: <glassfish_at_javadesktop.org>
Date: Mon, 05 Jul 2010 10:52:06 PDT

remington blade accessories
http://en.toppreise.ch/index.php?search=&cat=0&lp=&hp=&manu=1167&offset=50


I have a date field in my entity class like
@Column(name = "GIVEN_DATE", nullable = false)
  @Temporal(TemporalType.DATE)
  public Date getGivenDate() {
    return givenDate;
  }
  
The column is defined in Oracle DB as DATE type
 
 I am writing a JPAQL to retrive records based on a flag like
 1. if flag = true, then get records greater than today
 2. if flag = false, then get records between two dates given
 
 the JPQL is like
 
 ....
Date today = getToday();
if (allRecords) {
        qa.addCriteria("DATE(pos.givenDate) >= :fromDate");
        qa.addParameter("fromDate", today);

} else {
        qa.addCriteria("DATE(pos.givenDate) BETWEEN :fromDate AND :toDate");
        qa.addParameter("fromDate", fromDate);
        qa.addParameter("toDate", toDate);
}


My query is generated properly like

select pos from xyz AS pos where pos.givenDate >= :fromDate ---when flag = true
parameter value is {fromDate=Thu Jul 01 00:00:00 CEST 2010}

select pos from xyz AS pos where pos.givenDate >= :fromDate and pos.givenDate <= :toDate ---when flag = false
parameter value is {fromDate=Thu Jul 01 00:00:00 CEST 2010, toDate=Thu Jul 01 00:00:00 CEST 2010}

But the result is not as expected. It looks like it gives all results always. I am not getting records as i wish (greater than today or between two given date). When i googled, i feel the problem is the date sent from java is java Date format like Thu Jul 01 00:00:00 CEST 2010 whereas the data is DB is stored in Oracle Date format like 01-JUL-10.

So I feel we need to convert the date value before sending to oracle. I tried simpledateformat conversion, to_date, to_char, DATE() but they all work in native query and fails in JPAQL. Can someone suggest how to resolve the problem?
[Message sent by forum member 'kewlbuddy']

http://forums.java.net/jive/thread.jspa?messageID=476809