Java 使用休眠条件更新多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24847466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:11:37  来源:igfitidea点击:

Update multiple rows using hibernate Criteria

javahibernatepersistence

提问by ErrorNotFoundException

I am trying to run an update query which would look like this in sql:

我正在尝试运行一个更新查询,它在 sql 中看起来像这样:

update studentMaster set sess_status = 'G' where ACADEM_YEAR = COURSE_YEAR;

I am trying to re-create the query using Criterialike this:

我正在尝试使用以下标准重新创建查询:

public void updateSessionStatus() {
        Session sess = factory.openSession();
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            Criteria crit = sess.createCriteria(CollegeStudentsMaster.class);
            crit.add(Restrictions.eqProperty("academicYear", "courseYears"));
            CollegeStudentsMaster e = (CollegeStudentsMaster) crit.uniqueResult();
            e.setSessionStatus("G");
            sess.saveOrUpdate(e);
            tx.commit();
        } catch (HibernateException asd) {
            if (tx != null) {
                tx.rollback();
            }
            log.debug(asd.getMessage());
        } finally {
            sess.close();
        }
    }

This is not working because the rows which meet this Criteria are many, my unique result is the problem here I guess. How can I convert this into an update for all the rows that meet the Criteria. I do not want to use HQL query, I am rather doing it with Criteria.

这是行不通的,因为满足此条件的行很多,我猜我唯一的结果就是这里的问题。如何将其转换为所有符合条件的行的更新。我不想使用 HQL 查询,我宁愿使用 Criteria。

采纳答案by Bhushan

public void updateSessionStatus() {
        Session sess = factory.openSession();
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            Criteria crit = sess.createCriteria(CollegeStudentsMaster.class);
            crit.add(Restrictions.eqProperty("academicYear", "courseYears"));
            // Here is updated code
            ScrollableResults items = crit.scroll();
            int count=0;
            while ( items.next() ) {
                CollegeStudentsMaster e = (CollegeStudentsMaster)items.get(0);
                e.setSessionStatus("G");
                sess.saveOrUpdate(e);
                if ( ++count % 100 == 0 ) {
                    sess.flush();
                    sess.clear();
                }
            }
            tx.commit();
        } catch (HibernateException asd) {
            if (tx != null) {
                tx.rollback();
            }
            log.debug(asd.getMessage());
        } finally {
            sess.close();
        }
    }

It is always suggested that execute bulk operations very close to database and we do not need keep updated object in session unless they are required, Hence try to avoid load objects in session while executing bulk operations.

总是建议在非常接近数据库的情况下执行批量操作,除非需要,否则我们不需要在会话中保留更新的对象,因此在执行批量操作时尽量避免在会话中加载对象。