本文介绍了使用hibernate Criteria更新多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  update studentMaster set sess_status ='G 

我试图运行一个在sql中看起来像这样的更新查询: 'ACADEM_YEAR = COURSE_YEAR;

我试图用 Criteria 重新创建查询,如下所示:

  public void updateSessionStatus(){
Session sess = factory.openSession();
Transaction tx = null;
尝试{
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();




$ b $ p
$ b

这不起作用,因为满足这个标准的行很多,我的独特结果是我想这里的问题。
如何将其转换为符合标准的所有行的更新。我不想用HQL查询,我宁愿用Criteria来做。

  public void updateSessionStatus(){
Session sess = factory.openSession();
Transaction tx = null;
尝试{
tx = sess.beginTransaction();
Criteria crit = sess.createCriteria(CollegeStudentsMaster.class);
crit.add(Restrictions.eqProperty(academicYear,courseYears));
//这是更新的代码
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();


$ / code>

总是建议执行批量操作非常接近数据库,并且我们不需要在会话中保持更新的对象,除非它们是必需的。因此,尝试在执行批量操作时避免在会话中加载对象。

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

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

I am trying to re-create the query using Criteria like 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.

解决方案
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.

这篇关于使用hibernate Criteria更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:07
查看更多