Hibernate中的批量处理executeUpdate()

// 批量处理:JDBC

	@org.junit.Test
	public void jdbcBatch(){
		session=HibernateUtil.getSession();
		tx=session.beginTransaction();
		Work work=new Work(){
//匿名内部类、Work实现类
			@Override
			public void execute(Connection conn) throws SQLException {
		sql="update Emp set sal=sal+2";
		PreparedStatement pst=conn.prepareStatement(sql);
		pst.executeUpdate();				
			}			
		};
		session.doWork(work);
		tx.commit();
		HibernateUtil.closeSession();
	}
**// 批量处理:HQL**
	public void hqlBatch() {
		session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		hql = "update Emp set comm=50 where comm=0";
		Query query = session.createQuery(hql);
		int num = query.executeUpdate();
		System.out.println(num);
		tx.commit();
		HibernateUtil.closeSession();
	}

Hibernate中的批量处理executeUpdate()_第1张图片

你可能感兴趣的:(Hibernate)