简体   繁体   中英

mapping dates in hibernate and mysql

We have an app in which we are using hibernate with mysql db.

We have a db script import.sql which have some insert into statements and we also have some date fields in db like start_date end_date in which we are string dates in default format, that is,YYYY-MM-DD.

Now issue is at the time of retrieving/comparing dates hibernates showing strange behaviour for example suppose if we have a date 2012-01-30 then hibernate reads in proper format that is, Jan 30 2012, but if we have a date like 2012-02-06 then hibernate reads as June 02 2012. my DAO for comparing and retrieving result is as follows

public final List<Record> getPastRecords(final java.util.Date currentDate) {
List<Record> pastRecord =  session.createCriteria(Record.class)
    .add(Restrictions.lt("endTime", currentDate))
    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return pastRecord;
}

Any idea what I am doing wrong?

without detailed code explanation guessing what may be the problem is very hard through i guess it may be because of java.util.Date try to use java.sql.Date as when you call methods/constructors of libraries that deal with database better to use wrapper of java.util.Date which is java.sql.Date.

refer http://www.theresearchkitchen.com/archives/58

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM