Can anyone suggest me the solution of fetching data on condition of current date which i am passing in dao layer method of type system current date. for e.g I have to fetch data from table "X" which have a column "startdate" of Date type(Mysql) on the basis if that date is greater with the current date passed to the query in java. I have tried with java.util.Date but not working and also my requirement is not to used database specific function like curr() or Now(). I found a relative post but none helped.Is there no alternative apart from using JodaTime. Thanks.
1 Answer
In your Entity class mark 'startdate' field with
@Column(name = "startdate")
@Temporal(TemporalType.DATE)
private Date startdate;
And create query like this one:
@NamedQuery(name = "Entity.findAfterDate", query = "SELECT e FROM Entity e WHERE e.startdate >= :dateAfter")
In your EntityDAOImpl
public List<Entity> getEntitiesAfterDate(Date date) {
Query query = openSession().getNamedQuery("Entity.findAfterDate");
query.setParameter("dateAfter", date);
return query.list();
}