I am trying to retrieve the records from a table using criteria API for a particular date. Following is the info regarding the db table, configuration and code. My concern is that I could not able to read the records when the date is passed.
Appreciate if some help me out what's wrong with my approach.
DB table
CREATE TABLE `ws_access` ( `id` bigint(20) unsigned NOT NULL auto_increment, `user_id` bigint(20) NOT NULL, `ws_id` bigint(20) NOT NULL, `date_accessed` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
Hibernate Configuration
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="abc.hibernate.entity"> <class name="abc.hibernate.entity.WsAccess" table="ws_access"> <id name="id" column="id"> <generator class="assigned"/> </id> <property name="userKey" column="user_id"/> <property name="workSheetKey" column="ws_id"/> <property name="dateAccessed" column="date_accessed"/> </class>
</hibernate-mapping>
Code
public List<WsAccess> getInfo(long userid, long worksheetId) { List<WsAccess> data = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { Criteria criteria = session.createCriteria(WsAccess.class); criteria.add(Restrictions.eq("userKey", new Long(userid))); criteria.add(Restrictions.eq("workSheetKey", new Long(worksheetId)));
try { DateFormat format = new SimpleDateFormat("MM/dd/yyyy"); String curdate = format.format(new Date()); Date date = (Date)format.parse(curdate); criteria.add(Restrictions.eq("dateAccessed", date)); } catch (ParseException e) {
}
data = criteria.list(); } finally { if(session != null) { session.close(); } } return data; }
|