如何在hibernate中将列值设置为Arraylist

如何在hibernate中将列值设置为Arraylist,hibernate,Hibernate,在这里,我想从列表中设置两列值,这样我可以一次更新多行。我尝试了这个方法,但它不起作用 String hql = "UPDATE empDTO set empstatus =(:status) , empreason = (:reason) WHERE enpID=(:id) and createdOn =:CreatedOn "; Query query = session.createQuery(hql); query

在这里,我想从列表中设置两列值,这样我可以一次更新多行。我尝试了这个方法,但它不起作用

String hql = "UPDATE empDTO set empstatus =(:status) , empreason = 
            (:reason) WHERE enpID=(:id) and createdOn =:CreatedOn ";
            Query query = session.createQuery(hql);
            query.setParameterList("status", status);//status is list
            query.setParameterList("reason", reason);//reason is list
            query.setParameterList("id", id);//id is list
            query.setParameter("CreatedOn",new Date());
             query.executeUpdate();
            rowAffected = query.executeUpdate();
org.hibernate.exception.DataException:无法执行更新查询 位于org.hibernate.exception.sqlstatecoverter.convert(sqlstatecoverter.java:102) 位于org.hibernate.exception.jdbceptionhelper.convert(jdbceptionhelper.java:66)


原因:java.sql.SQLException:操作数应包含1列

我建议您执行以下操作

创建一个DTO来更新employeeUpdateDTO的列表

public class EmployeeUpdateDTO {
    private Integer empId;
    private String reason;
    private String status;

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
将此添加到您的实现中

    public void updateEmployee(List<EmployeeUpdateDTO> employeeUpdateDTOs) {
            String updateQuery = "UPDATE empDTO set empstatus =?,empreason =? WHERE enpID=? and createdOn =?";
            try {
                int[] updateCounts = jdbcTemplate.batchUpdate(updateQuery,
                        new BatchPreparedStatementSetter() {
                            public void setValues(PreparedStatement ps, int i) throws SQLException {

                                ps.setInt(1, employeeUpdateDTOs.get(i).empId);
                                ps.setString(2, employeeUpdateDTOs.get(i).getReason());
                                ps.setString(3, employeeUpdateDTOs.get(i).getStatus());
 ps.setDate(4, new java.sql.Date(System.currentTimeMillis())));
                            }

                            public int getBatchSize() {
                                return employeeUpdateDTOs.size();
                            }
                        });

            } catch (Exception e) {
                e.printStackTrace();

            }

        }
public void updateEmployee(列出employeeUpdateDTOs){
String updateQuery=“UPDATE empDTO set empstatus=?,emprason=?其中enpID=?和createdOn=?”;
试一试{
int[]updateCounts=jdbcTemplate.batchUpdate(updateQuery,
新的BatchPreparedStatementSetter(){
公共void setValues(PreparedStatement ps,int i)引发SQLException{
ps.setInt(1,employeeUpdateDTOs.get(i.empId));
ps.setString(2,employeeUpdateDTOs.get(i.getReason());
ps.setString(3,EmployeeUpdateTos.get(i.getStatus());
ps.setDate(4,新的java.sql.Date(System.currentTimeMillis());
}
public int getBatchSize(){
返回employeeUpdateDTOs.size();
}
});
}捕获(例外e){
e、 printStackTrace();
}
}

如果reason是一个列表,则子句
emprason=(:reason)
没有意义。身份也一样。员工只有一个状态,对吧,因此将员工的状态设置为列表没有意义。原因:java.sql.SQLException:操作数应包含1列,请参见更新question@JBNizet我很想知道在查询中的何处可以做什么?你应该首先解释一下你想要实现什么。用一个具体的例子。如果要更新N名员工,每个员工都有不同的原因和状态,则需要N个查询。或者更好,您需要加载这些N名员工,设置他们的权限,并让Hibernate为您更新数据库。HQL转换为SQL。您不能
更新emp set status='a','b','c',reason='x','y','z',其中id位于(1,2,3)
。这是无效的SQL。有效的SQL将是以下形式的3个查询:
updateempsetstatus='a',reason='x',其中id=1
。我必须使用哪个插件
SpringJDBC