-->
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.  [ 1 post ] 
Author Message
 Post subject: Date Set using Criteria API
PostPosted: Mon Oct 04, 2010 5:18 pm 
Newbie

Joined: Mon Oct 04, 2010 5:07 pm
Posts: 1
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;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.