Hibernate的批量处理
2014-01-08 23:32
260 查看
1. 批量插入
Hibernate的Session持有一个必选的一级缓存,数据操作的时候都将在Session级别的缓存区进行缓存。如果同时操作的数据量太大,将会抛出OutOfMemoryException异常(内存溢出)。所以,我们可以定时将Session缓存的数据刷入数据库,而不是一直在Session级别缓存。如下:
public class PersonManager{
public static void main(String[] args){
PersonManager mgr = new PersonManager();
mgr.testPerson();
HibernateUtil.sessionFactory.close();
}
private void testPerson(){
Session session = HibernateUtil.currentSession(); //HibernateUtil类如前面博客 Hibernate的映射(集合)
Transaction tx = session.beginTransaction();
//创建一个Person对象
for(int i=0;i< 10000;i++){
Person p = new Person();
p.setName("Name"+i);
session.save(p);
//每当累加器是20的倍数时,将Session中数据刷入数据库,并清空Session缓存
if(i%20 ==0){
session.flush();
session.clear();
}
}
tx.commit();
HibernateUtil.closeSession();
}
}
2. DML风格的批量更新
public class PersonManager{
public static void main(String[] args){
PersonManager mgr = new PersonManager();
mgr.testPerson();
HibernateUtil.sessionFactory.close();
}
private void testPerson(){
Session session = HibernateUtil.currentSession(); //HibernateUtil类如前面博客 Hibernate的映射(集合)
Transaction tx = session.beginTransaction();
String hqlUpdate = "update Person set name=:newName";
int updatedEntities = session.createQuery(hqlUpdate).setString("newName", "新名字").executeUpdate();
tx.commit();
HibernateUtil.closeSession();
}
}
3. DML风格的批量删除
public class PersonManager{
public static void main(String[] args){
PersonManager mgr = new PersonManager();
mgr.testPerson();
HibernateUtil.sessionFactory.close();
}
private void testPerson(){
Session session = HibernateUtil.currentSession(); //HibernateUtil类如前面博客 Hibernate的映射(集合)
Transaction tx = session.beginTransaction();
String hqlDelete = "delete Person";
int deletedEntities = session.createQuery(hqlDelete).executeUpdate();
tx.commit();
HibernateUtil.closeSession();
}
}
Hibernate的Session持有一个必选的一级缓存,数据操作的时候都将在Session级别的缓存区进行缓存。如果同时操作的数据量太大,将会抛出OutOfMemoryException异常(内存溢出)。所以,我们可以定时将Session缓存的数据刷入数据库,而不是一直在Session级别缓存。如下:
public class PersonManager{
public static void main(String[] args){
PersonManager mgr = new PersonManager();
mgr.testPerson();
HibernateUtil.sessionFactory.close();
}
private void testPerson(){
Session session = HibernateUtil.currentSession(); //HibernateUtil类如前面博客 Hibernate的映射(集合)
Transaction tx = session.beginTransaction();
//创建一个Person对象
for(int i=0;i< 10000;i++){
Person p = new Person();
p.setName("Name"+i);
session.save(p);
//每当累加器是20的倍数时,将Session中数据刷入数据库,并清空Session缓存
if(i%20 ==0){
session.flush();
session.clear();
}
}
tx.commit();
HibernateUtil.closeSession();
}
}
2. DML风格的批量更新
public class PersonManager{
public static void main(String[] args){
PersonManager mgr = new PersonManager();
mgr.testPerson();
HibernateUtil.sessionFactory.close();
}
private void testPerson(){
Session session = HibernateUtil.currentSession(); //HibernateUtil类如前面博客 Hibernate的映射(集合)
Transaction tx = session.beginTransaction();
String hqlUpdate = "update Person set name=:newName";
int updatedEntities = session.createQuery(hqlUpdate).setString("newName", "新名字").executeUpdate();
tx.commit();
HibernateUtil.closeSession();
}
}
3. DML风格的批量删除
public class PersonManager{
public static void main(String[] args){
PersonManager mgr = new PersonManager();
mgr.testPerson();
HibernateUtil.sessionFactory.close();
}
private void testPerson(){
Session session = HibernateUtil.currentSession(); //HibernateUtil类如前面博客 Hibernate的映射(集合)
Transaction tx = session.beginTransaction();
String hqlDelete = "delete Person";
int deletedEntities = session.createQuery(hqlDelete).executeUpdate();
tx.commit();
HibernateUtil.closeSession();
}
}
相关文章推荐
- windows socket api 封装
- JS判断IP合法性
- BeanUtils
- JS调用CS里的带参方法
- POJ 1011 Sticks DFS 剪枝
- 枚举
- PHP的 Mysqli扩展库的多语句执行
- Maven插件之properties-maven-plugin
- 启动和结束进程
- Apache Struts 2
- PHP基础文件下载类的简单封装
- Edit Distance
- 1108:棋盘游戏
- 最新报道:广州传智播客PHP培训确定要开班拉
- 关于ios中的文本操作-简介
- 确保当前只有一个窗口进程
- iOS 3D UI---CALayer的transform扩展
- PHP基础封装简单的MysqliHelper类
- [leetcode]Search a 2D Matrix
- c++中的类模板