如何在hibernate中更新值数组

问题描述 投票:0回答:1

我正在尝试使用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;
    }
java hibernate orm
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.