hibernate中的时间查询

来源:百度知道 编辑:UC知道 时间:2024/06/15 20:13:35
想请教hibernate时间查询的具体代码.数据库字段是mysql的date类型(yy-mm-dd).想查询在某日到某日内的记录.
代码如下但是执行到第二行就出错了.错在哪?
Session s=HibernateSessionFactory.getSession();
Query query=s.createQuery("from UserBusiness as a where a.month between '2009-04-01' and '2009-04-07'");
List result=query.list();
Iterator it=result.iterator();
while(it.hasNext()){
UserBusiness us=(UserBusiness)it.next();
System.out.println(us.getUserid());
System.out.println(us.getBusinessId());
System.out.println(us.getMonth());
}

findSql += " AND file.receiptDate BETWEEN :startTime AND :endTime ";
map.put("startTime", c.getStartTime());
map.put("endTime", c.getEndTime());

看这个代码应该就明白了吧

用hql就可以了,根据你日期类型拼接字符串,这个最简单,要是有架构上的需求可以用类似struts之类框架做类型校验保证数据流到持久化曾都不回有错误数据,直接把出错可能挡在数据库之外