欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2010.01.13——hql 的时间问题 弄了一下午 郁闷

程序员文章站 2022-04-18 09:32:34
...
2010.01.13——hql 的时间问题 弄了一下午 郁闷
Dao的方法如下
public Double getYuliang(String stcd, Date startTime, Date endTime) {   
CustomerContextHolder.setCustomerType("query_su9921");
String hql = "select sum(s.DYP) from ST_PPTN_R s where s.STCD=? " +
"and s.TM>to_date(?,'yyyy-MM-dd HH24:mi:ss') " +
"and s.TM<to_date(?,'yyyy-MM-dd HH24:mi:ss')";
System.out.println(startTime);
System.out.println(endTime);
String s = FormatDate.formatDate(startTime, "yyyy-MM-dd HH:mm:ss");
String e = FormatDate.formatDate(endTime, "yyyy-MM-dd HH:mm:ss");
System.out.println(s);
System.out.println(e);
Object[] values = {stcd,s,e};

log.info(hql);
return (Double)this.template.find(hql,values).get(0);
}

测试的方法:
@Test  
public void testGetYuliang() throws ParseException {
String stcd = "62518600";
YuliangDao yuliangDao = (YuliangDao) ctx.getBean("yuliangDao");
Date startTime = new Date();
System.out.println(startTime);
Calendar endTime = Calendar.getInstance();
endTime.setTime(startTime);
endTime.add(Calendar.YEAR, 1);
System.out.println(endTime.getTime());

Double num = yuliangDao.getYuliang(stcd, startTime, endTime.getTime());
System.out.println(num);
Assert.assertNotNull(num);
}

junit的主要错误信息如下:
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: TM of: com.pojo.rthi.ST_PPTN_R [select sum(s.DYP) from com.pojo.rthi.ST_PPTN_R s where s.STCD=? and s.TM>to_date(?,'yyyy-MM-dd HH24:mi:ss') and s.TM<to_date(?,'yyyy-MM-dd HH24:mi:ss')]; nested exception is org.hibernate.QueryException: could not resolve property: TM of: com.pojo.rthi.ST_PPTN_R [select sum(s.DYP) from com.pojo.rthi.ST_PPTN_R s where s.STCD=? and s.TM>to_date(?,'yyyy-MM-dd HH24:mi:ss') and s.TM<to_date(?,'yyyy-MM-dd HH24:mi:ss')]

竟然说找不到TM 郁闷 我的映射肯定是没错,这个问题弄了一个下午,到最后原因竟然是:
把s.TM 改为TM就对了 原因不明 终于解决了 我是内牛满面啊

ps:另外那个dao的方法可以简单化为:
public Double getYuliang(String stcd, Date startTime, Date endTime) {
CustomerContextHolder.setCustomerType("query_su9921");
String hql = "select sum(s.DYP) from ST_PPTN_R s where s.STCD= ? and TM between ? and ?";
log.info(startTime);
log.info(endTime);
Object[] values = {stcd,startTime,endTime};
log.info(hql);
return (Double)getHibernateTemplate().find(hql,values).get(0);
}
就是说可以直接用Date对象来注入
相关标签: DAO