Hibernate四 批量处理

Hibernate批量处理
一 批量插入
将很多条记录插入数据库时,Hibernate通常会采用以下做法:
public void test() {
for(int i=0;i<1000;i++){
Person p =new Person("admin"+i,1234+i,new Date());
session.persist(p);
System.out.println(p);
}
}
但是随着这个程序的运行,会在某个时刻失败,并抛出OutOfMemoryException,这是因为Hibernate的Session持有一个必选的一级缓存,所有的Person实例都将在这个Session级别的缓存区存放。
解决方案:定时将Session缓存的数据刷入数据库。
public void test() {
for(int i=0;i<1000;i++){
Person p =new Person("admin"+i,1234+i,new Date());
session.persist(p);
if(i%10==0){
session.flush(); //可以立即同步持久化状态数据到数据库表
session.clear();
}
System.out.println(p);
}
tx.commit();
}
二 批量更新
上面的方法依然适用,应该使用scroll()方法,从而充分利用服务器端游标所带来的性能优势。
public class myTest {
public static void main(String[] args) {
Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
ScrollableResults persons=session.createQuery("from Person")
.setCacheMode(CacheMode.IGNORE)
.scroll(ScrollMode.FORWARD_ONLY);
int count=0;
while(persons.next()){
Person p=(Person) persons.get(0);
p.setName("name:"+count);
if(++count%10==0){
session.flush();
session.clear();
}
}
tx.commit();
session.close();
}
}
但是这种方式效率不高,因为要先执行查询语句,在执行数据更新。为了避免这种情况,Hibernate提供了一种类似于DML语句的批量更新、批量删除的HQL语法。
三 DML风格的批量更新/删除
语法格式:
update | delete from? <ClassName> [where where_conditions]
注意:from关键字可选,from自居中只能有一个类名,可以在该类名后指定别名。不能在批量HQL语句中使用连接,显式或隐式的都不行,但可以在where子句中使用子查询。
public class myTest {
public static void main(String[] args) {
Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
String hqlUpdate="update Person p set name= :newName";
int updateEntities=session.createQuery(hqlUpdate)
.setString("newName","新名字")
.executeUpdate();
tx.commit();
session.close();
}
}
批量删除:
public class myTest {
public static void main(String[] args) {
Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
String hqlDelete="delete Person";
int updateEntities=session.createQuery(hqlDelete)
.executeUpdate();
tx.commit();
session.close();
}
}

原文地址:https://www.cnblogs.com/lyy-2016/p/5726965.html