org.springframework.orm.hibernate3.HibernateQueryException中的一個小異常
阿新 • • 發佈:2019-01-11
在使用Hibernate的時候,出現了這樣的一個錯誤:
org.springframework.orm.hibernate3.HibernateQueryException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 2; nested exception is org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 2
這個錯誤說是因為佔位符數量不匹配的原因;
dao層:
@Override public List<T> findCollectionByConditionNoPage(String hqlWhere,final Object[] params, LinkedHashMap<String, String> orderby) { /** * 組織HQL語句的Where條件 * select * from elec_text o where 1=1 放置DAO層 and o.textName like '%張%' 放置Service層 and o.textRemark like '%李%' order by o.textDate desc , o.textName asc */ String hql = "from " + entity.getSimpleName() + " o where 1=1"; //組織排序條件 String hqlOrderBy = this.orderByCondition(orderby); hql = hql + hqlWhere + hqlOrderBy; final String finalHql = hql; List<T> list = (List<T>)this.getHibernateTemplate().execute(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery(finalHql); setParams(query,params); return query.list(); } }); return list; } /** * @Name: setParams * @Description: 對where條件中的引數設定引數值 * @Parameters: Object[] params 引數值 * @Return: 無 */ private void setParams(Query query,Object[] params) { for(int i=0;params!=null && i<params.length;i++){ query.setParameter(i, params[i]); } } /** * @Name: orderByCondition * @Description: 組織排序條件 * @Parameters: LinkedHashMap<String, String> orderby 排序條件 * @Return: String 排序語句的字串 */ private String orderByCondition(LinkedHashMap<String, String> orderby) { StringBuffer buffer = new StringBuffer(""); if(orderby!=null){ buffer.append(" order by "); for(Map.Entry<String, String> map:orderby.entrySet()){ buffer.append(" " + map.getKey() + " " + map.getValue() + ","); } buffer.deleteCharAt(buffer.length()-1); } return buffer.toString(); }
server層:
/** * @Name: findCollectionByConditionNoPage * @Description: 使用 查詢條件,查詢列表的集合(不分頁) * @Parameters: ElecTextForm elecTextForm VO物件 * @Return: List<ElecText> 列表集合 */ @Override public List<ElecText> findCollectionByConditionNoPage( ElecTextForm elecTextForm) { /** * 組織HQL語句的Where條件 * select * from elec_text o where 1=1 放置DAO層 and o.textName like '%張%' 放置Service層 and o.textRemark like '%李%' order by o.textDate desc , o.textName asc */ String hqlWhere = ""; List<String> paramsList = new ArrayList<String>(); if(elecTextForm!=null && StringUtils.isNotBlank(elecTextForm.getTextName())){ hqlWhere += " and o.textName like ?"; paramsList.add("%"+elecTextForm.getTextName()+"%"); } if(elecTextForm!=null && StringUtils.isNotBlank(elecTextForm.getTextRemark())){ hqlWhere += " and o.textRemark like ?"; paramsList.add("%"+elecTextForm.getTextRemark()+"%"); } Object [] params = paramsList.toArray(); /** * 組織排序語句 * order by o.textDate desc , o.textName asc */ LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>(); orderby.put("o.textDate", "desc"); orderby.put("o.textName", "asc"); //查詢列表 List<ElecText> list = elecTextDao.findCollectionByConditionNoPage(hqlWhere,params,orderby); for(int i=0;list!=null && i<list.size();i++){ ElecText elecText = list.get(i); System.out.println(elecText.getTextName() + " " + elecText.getTextRemark()); } return null; }
測試類:
/**
* 通過查詢條件,查詢物件的列表集合
* 模仿Action層
* */
@Test
public void findCollection(){
IElecTextService elecTextService = (IElecTextService)ServiceProvider.getService(IElecTextService.SERVICE_NAME);
//例項化PO物件,賦值,執行儲存
ElecTextForm elecTextForm = new ElecTextForm();
elecTextForm.setTextName("張");
elecTextForm.setTextRemark("高");
//返回list集合
List<ElecText> list = elecTextService.findCollectionByConditionNoPage(elecTextForm);
}
原因是我的資料庫匹配的資料只有一條資訊,
當我再插入一條匹配資訊的時候錯誤就消失了;