org.springframework.orm.hibernate3.HibernateQueryException中的一个小异常

版权声明:原创文章,转载请声明原处 https://blog.csdn.net/qq_41723615/article/details/85336262

在使用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);
		
	}

原因是我的数据库匹配的数据只有一条信息,

当我再插入一条匹配信息的时候错误就消失了;

猜你喜欢

转载自blog.csdn.net/qq_41723615/article/details/85336262

相关文章