关于hibernate中ordinal parameter mismatch的解决方法
[color=blue]今天遇到了hibernate中的ordinal parameter mismatch的这个错误,最后在网上查也没查出什么好的结果,最后看到一个网上的帖子提供了一些思路,但也没给出具体的实现方法,我这里贴出来供以后的新手借鉴下,我也是个新手。呵呵,不足之处请大家指教
我的model类Calldetail里有两个字段:u_callresult和u_callstate,因为它们的值是通过oracle里的存储过程进行入库的,所以我在一开始的hql语句里是这样写的:
String hql = "from Calldetail where u_accountid=? and u_callresult='1' and u_callstate='1'";
这样写的结果是在执行查询语句时hibernate就报了ordinal parameter mismatch这个错误,现在是用Criteria通过类的加载来实现查询,写法如下:
/** * 通过帐号编号得到通话记录* @param accountid* @return */public List getCalldetailByAccountid(int accountid){ Criteria criteria = this.getSession().createCriteria(Calldetail.class); //加载model类 //添加查询条件 criteria.add(Expression.eq("accounts.u_accountid",accountid)); criteria.add(Expression.eq("u_callresult","1")); criteria.add(Expression.eq("u_callstate","1")); List list = criteria.list(); return list;}
/*** 根据客户选择的日期查询帐户信息* @param hts* @param hte* @param accountid* @return*/public List getCalldetailByDate(String hts,String hte,int accountid){String sda = hts + " " + "00:00:00";String eda = hte + " " + "23:59:59";Calendar start_calendar = ParseDate.parseDateTime(sda); //parseDate是一个将字符串格式的转化为Calendar的一个方法Date start_date = start_calendar.getTime();//System.out.println(start_date);Calendar end_calendar = ParseDate.parseDateTime(eda);Date end_date = end_calendar.getTime();//System.out.println(end_date);Criteria criteria = this.getSession().createCriteria(Calldetail.class); criteria.add(Expression.eq("accounts.u_accountid",accountid));criteria.add(Expression.eq("u_callresult","1"));criteria.add(Expression.eq("u_callstate","1"));criteria.add(Expression.between("u_ostarttime", start_date, end_date));List list = criteria.list();return list;}
package com.itfm.webcall.util;import java.util.Calendar;import java.util.GregorianCalendar;public class ParseDate {/** * 将字符串格式的转换为Calendar类型 * @param baseDate * @return */public static Calendar parseDateTime(String baseDate){ Calendar cal = null; cal = new GregorianCalendar(); int yy = Integer.parseInt(baseDate.substring(0, 4)); int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1; int dd = Integer.parseInt(baseDate.substring(8, 10)); int hh = 0; int mi = 0; int ss = 0; if(baseDate.length() > 12){ hh = Integer.parseInt(baseDate.substring(11, 13)); mi = Integer.parseInt(baseDate.substring(14, 16)); ss = Integer.parseInt(baseDate.substring(17, 19)); } cal.set(yy, mm, dd, hh, mi, ss); return cal; }}[/color]