本文介绍了setParameterList()不返回完整的结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要有效地制作我的小型弹簧工程。所以我使用

01) setParameterList() 中的问题

List<Department> listDeptmntId = reportService.listDepartmentID(companyId); //list of objects

String hql = "select s.department.departmentName, g.dateTime from Gauge g inner join g.survey s where s.department in (:dpts)";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setParameterList("dpts",listDeptmntId);

The query works fine. But this return only one (first object in listDeptmntId list) resultset, does not return other results.

I tries to pass integer list like [1,2,3] using following method also not working.

List<Integer> dptIds=listDeptmntId.stream().map((Department::getDepartmentId()).collect(Collectors.toList());

02) Question in setParameter()

int cId=10;
String hql="...... companyId=:id" 
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setParameter("id",cId);

Sometime when I use parameter passing ("=:") , it does not work for Integers . But directly setting variable to query like following is working

int cId=10;
String hql="...... companyId="+cId
Query query = sessionFactory.getCurrentSession().createQuery(hql);

My code may be wrong because I'm going through ebooks and referring materials to do the project. Thank you in advance.

解决方案

I generally do IN(?, ..., ?) using an java.sql.Array.

long[] deptIds = listDeptmntId.toArray(new long[listDeptmntId.size()];
java.sql.Array array = conn.createArrayOf("LONG", deptIds);
query.setPameter("depts", array);

这篇关于setParameterList()不返回完整的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:55