如何在hiberbate中更新值数组

时间:2017-01-18 11:16:39

标签: java hibernate orm

我正在尝试使用Hibernate更新为值数组。当我这样做时,行会更新,但每次我将行受影响的值设置为1.为什么?

public int promoteStudentsToNextClass(String[] studentsRefrenceIDS,String promotedClassValue) { 
        int r = 0;
        Session session = null;
        Transaction tx = null;
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            tx = session.beginTransaction();
            String hql = "update StudentDTO set studentClassName=? where studentReferenceID=?";
            Query query = session.createQuery(hql);
            for (int i = 0; i < studentsRefrenceIDS.length; i++) {          
                query.setParameter(0, promotedClassValue);
                query.setParameter(1, studentsRefrenceIDS[i]);  
                 r=query.executeUpdate();               
                    System.out.println("row updated "+r);
            }       


            tx.commit();
        return r;
        } catch (HibernateException he) {
            he.printStackTrace();
            tx.rollback();
        }
        return r;
    } 

即使没有正确地给出行影响值,这也不是批量的

public boolean updateStudentsAttendance(StudentAttendanceDTO attendanceDTO) {
        Date date =new Date();
        attendanceDTO.setModifiedBy("Admin");
        attendanceDTO.setModifiedOn(date);
        Session session = null;
        Transaction tx = null;
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            tx = session.beginTransaction();
            String hql = "update StudentAttendanceDTO set attendanceStatus=?,reasonForAbsent=?,modifiedBy=?,modifiedOn=? where className=? and studentReferenceID=?  ";
            Query query = session.createQuery(hql);
            query.setParameter(0, attendanceDTO.getAttendanceStatus());
            query.setParameter(1, attendanceDTO.getReasonForAbsent());
            query.setParameter(2, attendanceDTO.getModifiedBy());
            query.setParameter(3, attendanceDTO.getModifiedOn());
            query.setParameter(4, attendanceDTO.getClassName());
            query.setParameter(5, attendanceDTO.getStudentReferenceID());

            int r=query.executeUpdate();            
            System.out.println("row updated "+r);           
            tx.commit();
            if(r==1)
            {
                return true;
            }

        } catch (HibernateException he) {
            he.printStackTrace();
            tx.rollback();
        }
        return false;
    }

1 个答案:

答案 0 :(得分:0)

每次执行批量更新(通过使用executeUpdate()方法)时,您都会绕过内存中的PersistenceContext(jpa术语)并直接进入数据库并执行查询。

对于诸如save(),saveOrUpdate()之类的操作,或者仅使用find()获取实体并对其进行修改,情况并非如此。那些操作PersistenceContext而不直接进入数据库。当完成事务方法处理并且无论何时进行任何更改时,都会自动完成。

如果您希望在给定的事务中只对数据库进行一次更新,请尝试执行以下操作:

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
 session = sessionFactory.openSession();
 tx = session.beginTransaction();
 for (int i = 0; i < studentsRefrenceIDS.length; i++) { 
    StudentDTO student = session.find(StudentDTO.class, studentsRefrenceIDS[i]);
    student.setStudentClassName(promotedClassValue);
 }

tx.commit();