package org.monday.app; import org.hibernate.Session; import org.hibernate.Transaction; import org.monday.domain.User; import org.monday.util.HibernateUtil; public class UserManager { public static void main(String[] args) { // batchInsert(); // batchUpdate(); batchDelete(); } /** * 批量添加 */ public static void batchInsert() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); for (int i = 1,n=10000; i <= n; i++) { User u = new User(); u.setName("sql" + i); session.save(u); // save if (i % 20 == 0) { session.flush(); // 将Session缓存的数据写入到数据库 session.clear(); // 手动清空Session缓存 } } tx.commit(); HibernateUtil.closeSession(); System.out.println("ok"); /** * <!-- 关闭Hibernate的二级缓存 --> <property name="hibernate.cache.use_second_level_cache">false</property> 除了Session的缓存要关闭外,最好将SessionFactory的二级缓存也关闭了 */ } /** * 批量更新 */ public static void batchUpdate() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); String hql = "update User set name = ? where id<=?"; int count = session.createQuery(hql).setParameter(0, "hql").setParameter(1, 10).executeUpdate(); tx.commit(); HibernateUtil.closeSession(); System.out.println("update:" + count); } /** * 批量删除 */ public static void batchDelete() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); String hql = "delete from User where id<=?"; int count = session.createQuery(hql).setParameter(0, 10).executeUpdate(); tx.commit(); HibernateUtil.closeSession(); System.out.println("delete:" + count); } }
package org.monday.util; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { /** 声明SessionFactory对象 */ private static final SessionFactory sf; /** * 静态块创建SessionFactory对象 */ static { try { Configuration cfg = new Configuration().configure(); sf = cfg.buildSessionFactory(); } catch (Throwable e) { System.out.println("创建SessionFactory失败"); throw new ExceptionInInitializerError(e); } } /** 声明ThreadLocal对象绑定Session */ private static final ThreadLocal<Session> session = new ThreadLocal<Session>(); /** * 获取Session * * @return 当前邦定到ThreadLocal的Session * @throws HibernateException */ public static Session getSession() throws HibernateException { Session s = session.get(); if (s == null) { s = sf.openSession(); session.set(s); } return s; } /** * 关闭Session * * @throws HibernateException */ public static void closeSession() throws HibernateException { Session s = session.get(); if (s != null) { s.close(); } session.set(null); } }
相关推荐
看完该文件你就可以最高效率的批量处理数据
高效的进行批量数据的处理方案,留作参考。
主要介绍了Hibernate批量处理海量数据的方法,较为详细的分析了Hibernate批量处理海量数据的原理与相关实现技巧,需要的朋友可以参考下
很多人都对Java在批量数据的处理方面是否是其合适的场所持有怀疑的念头,由此延伸,...下面以Hibernate为例来做为说明,假如我们真的不得不在Java中使用Hibernate来对数据进行批量处理的话。 向数据库插入100 000条数据
在Hibernate中处理批量更新和批量删除
在Hibernate应用中如何处理批量更新和批量删除
主要介绍了hibernate批量操作,结合实例形式分析了Hibernate实现批量插入,更新及删除等操作的具体实现技巧,需要的朋友可以参考下
jsp Hibernate批量更新和批量删除处理代码.docx
Hibernate处理1-N关系时保存技巧、Hibernate缓存机制、Hibernate批量处理数据、Hibernate三种继承映射策略、hibernate映射体系、Hibernate主键生成策略、持久层DAO设计建议、基于xml文件的bean、使用HibernateAPI在...
批量更新是指在一个事务中更新大批量数据,批量删除是指在一个事务中删除大批量数据。
2022年Hibernate下数据批量处理Java教程.docx
不建议用Hibernate,它的insert效率实在不搞,不过最新版本的Hibernate似乎已经在批量处理的时候做过优化了,设置一些参数如batch_size,不过性能我没有测试过
Hibernate完全以面向对象的方式来操作数据库,当程序里以面向对象的方式操作持久化对象时,将被自动转换为对数据库的操作。...为了面对这种批量处理的场景,Hibernate提供了批量处理的解决方案。下面分别
主要介绍了Hibernate管理Session和批量操作的技巧,包括Hibernate管理Session、批量处理数据等的常用技巧及注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下
Hibernate下数据批量处理解决方案
13. 批量处理(Batch processing) 13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) ...
代码基于 Hibernate4.x 录制。内容涵盖安装 Hibernatetools 插件、Session 核心方法、持久化对象生命周期、对象关系映射(1-n、1-1、n-n、继承映射)、检索...Session、批量处理等 Hibernate 企业级开发的核心技术。
HIbernate完整文档,有教程有截图,也有详细步骤,其中包括ORM思想介绍、框架原理、原理图、核心对象、核心配置文件、关联关系、映射文件、批量处理、事务控制、Hibernate缓存以及注解等
13. 批量处理(Batch processing) 13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) ...