01.批量插入数据

  步骤一.创建实体类,Dept和Emp

  

/**
* 员工类
* @author Administrator
*
*/
public class Emp {
private Integer empId;
private String empName;
private Dept dept;
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
} }
/*
* 部门实体类
*/
public class Dept {
//不问标号
private Integer deptNo;
private String deptName;
//关联一个员工集合
private Set<Emp> emps=new HashSet<Emp>(); public Set<Emp> getEmps() {
return emps;
}
public void setEmps(Set<Emp> emps) {
this.emps = emps;
}
public Integer getDeptNo() {
return deptNo;
}
public void setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
} }

  步骤二.创建Dept.hbm.xml和Emp.hbm.xml小配置文件

<!--Dept.hbm.xml-->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.a.entity">
<class name="Dept" table="Dept" lazy="false">
<id name="deptNo">
<generator class="sequence">
<param name="sequence">SEQ_Student</param>
</generator>
</id>
<property name="deptName"></property>
<set name="emps" cascade="save-update" lazy="extra">
<key column="deptNo"></key> <!-- 多的一方的外建 -->
<one-to-many class="Emp" />
</set>
</class>
</hibernate-mapping>
<!--Emp.hbm.xml-->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.a.entity">
<class name="Emp" table="Emp">
<id name="empId">
<generator class="sequence">
<param name="sequence">SEQ_Student</param>
</generator>
</id>
<property name="empName"></property>
<!-- 植入一个Dept对象 多对一 -->
<many-to-one name="dept" class="Dept" column="deptNo"></many-to-one>
</class>
</hibernate-mapping>

  步骤三.创建大配置文件hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings 数据库连接设置 -->
<!-- 驱动类 -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<!-- url地址 -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">Hibernate</property>
<property name="connection.password">orcl</property>
<!-- SQL dialect (SQL 方言) -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!--在控制台打印后台的SQL语句 -->
<property name="show_sql">true</property>
<!-- 格式化显示SQL -->
<!-- <property name="format_sql">true</property> -->
<!-- 自动生成表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 关联小配置 -->
<mapping resource="cn/a/entity/Dept.hbm.xml" />
<mapping resource="cn/a/entity/Emp.hbm.xml" />
</session-factory>
</hibernate-configuration>

  以上的步骤我们不做详细的讲解了,主要看测试的代码

/*
* 使用HQL语句批量处理数据
*/
public class HQLUpdateData {
/*
*批量添加数据
*/
@Test
public void addTest()
{
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
Query query = session.createQuery("insert into Dept(deptName) select d.deptName||d.deptNo from Dept d where d.deptNo>0");
int count = query.executeUpdate();
System.out.println(count);
tx.commit();
HibernateUtil.CloseSession();
}

通过HQL来进行批量操作

Hibernate3中的HQL(Hibernate Query Language,Hibernate查询语言)不仅可以检索数据,还可以用于进行批量更新、删除和插入数据。批量操作实际上直接在数据库中完成,所处理的数据不会被保存在Session的缓存中,因此不会占用内存空间。
Query.executeUpdate()方法和JDBC API中的PreparedStatement.executeUpdate()很相似,前者执行用于更新、删除和插入的HQL语句,而后者执行用于更新、删除和插入的SQL语句。

1.批量更新数据
以下程序代码演示通过HQL来批量更新Customer对象:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlUpdate =
"update Customer c set c.name = :newName where c.name = :oldName";
int updatedEntities = session.createQuery( hqlUpdate )
.setString( "newName", "Mike" )
.setString( "oldName", "Tom" )
.executeUpdate();

tx.commit();
session.close();
以上程序代码向数据库发送的SQL语句为:
update CUSTOMERS set NAME="Mike" where NAME="Tom"

2.批量删除数据
Session的delete()方法一次只能删除一个对象,不适合进行批量删除操作。以下程序代码演示通过HQL来批量删除Customer对象:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlDelete = "delete Customer c where c.name = :oldName";
int deletedEntities = session.createQuery( hqlDelete )
.setString( "oldName", "Tom" )
.executeUpdate();
tx.commit();
session.close();
以上程序代码向数据库提交的SQL语句为:
delete from CUSTOMERS where NAME="Tom"

3.批量插入数据
插入数据的HQL语法为:
insert into EntityName properties_list select_statement
以上EntityName表示持久化类的名字,properties_list表示持久化类的属性列表,select_statement表示子查询语句。
HQL只支持insert into ... select ... 形式的插入语句,而不支持"insert into ... values ... "形式的插入语句。
下面举例说明如何通过HQL来批量插入数据。假定有DelinquentAccount和Customer类,它们都有id和name属性,与这两个类对应的表分别为DELINQUENT_ACCOUNTS和CUSTOMERS表。DelinquentAccount.hbm.xml和Customer.hbm.xml文件分别为这两个类的映射文件。以下代码能够把CUSTOMERS表中的数据复制到DELINQUENT_ACCOUNTS表中:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlInsert = "insert into DelinquentAccount (id, name) select c.id, c.name from Customer c where c.id>1";
int createdEntities = s.createQuery( hqlInsert )
.executeUpdate();
tx.commit();
session.close();

以上程序代码向数据库提交的SQL语句为:
insert into DELINQUENT_ACCOUNTS(ID,NAME) select ID,NAME from CUSTOMERS where ID>1

9.4.4 直接通过JDBC API来进行批量操作

当通过JDBC API来执行SQL insert、update和delete语句时,SQL语句中涉及到的数据不会被加载到内存中,因此不会占用内存空间。
以下程序直接通过JDBC API来执行用于批量更新的SQL语句:
Transaction tx = session.beginTransaction();
//获得该Session使用的数据库连接
java.sql.Connection con=session.connection();
//通过JDBC API执行用于批量更新的SQL语句
PreparedStatement stmt=con.prepareStatement("update CUSTOMERS set AGE=AGE+1 "
+"where AGE>0 ");
stmt.executeUpdate();

tx.commit();

以上程序通过Session的connection()方法获得该Session使用的数据库连接,然后通过它创建PreparedStatement对象并执行SQL语句。值得注意的是,应用程序仍然通过Hibernate的Transaction接口来声明事务边界。
值得注意的是,在Hibernate3中,尽管Session的connection()方法还存在,但是已经被废弃,不提倡使用了,不过Hibernate3提供了替代方案:org.hibernate.jdbc.Work接口表示直接通过JDBC API来访问数据库的操作,Work接口的execute()方法用于执行直接通过JDBC API来访问数据库的操作:
public interface Work {
//直接通过JDBC API来访问数据库的操作
public void execute(Connection connection) throws SQLException;
}
Session的doWork(Work work)方法用于执行Work对象指定的操作,即调用Work对象的execute()方法。Session会把当前使用的数据库连接传给execute()方法。

以下程序演示了通过Work接口以及Session的doWork()方法来执行批量操作的过程:
Transaction tx=session.beginTransaction();
//定义一个匿名类,实现了Work接口
Work work=new Work(){
public void execute(Connection connection)throws SQLException{
//通过JDBC API执行用于批量更新的SQL语句
PreparedStatement stmt=connection
.prepareStatement("update CUSTOMERS set AGE=AGE+1 "
+"where AGE>0 ");
stmt.executeUpdate();
}
};

//执行work
session.doWork(work);
tx.commit();

最新文章

  1. Python程序高效地调试
  2. PHP中类的继承和构造函数的继承
  3. CentOS利用inotify+rsync实现文件同步
  4. Logic and Fault simulation
  5. spring+websocket整合
  6. C# [STAThread]
  7. hibernate 单元測试框架
  8. Top K问题的两种解决思路
  9. ICommand.CanExecuteChanged事件订阅对象的变化
  10. CodeBlocks使用小技巧
  11. 阅读笔记4 我是一只IT小小鸟
  12. python 图像处理(从安装Pillow开始)
  13. Java写 插入 选择 冒泡 快排
  14. Oracle批量执行SQL语句
  15. ExtJs GridPanel 给表格行或者单元格自定义样式
  16. PG覆盖率检查
  17. 20 几个知名公司的 Java 面试题汇总
  18. 织梦导航 currentstyle 点击li添加class类 样式
  19. SCOI 股票交易 单调队列优化dp
  20. Python-Jenkins API使用

热门文章

  1. UDS(ISO14229-2006) 汉译(No.6 应用层服务)
  2. CSS布局基础——BFC
  3. JavaScript基本语法(一)
  4. 你的USB设备还安全吗?USB的安全性已从根本上被打破!
  5. iOS应用中的相关正则及验证
  6. Windows平台下利用APM来做负载均衡方案 - 负载均衡(下)
  7. vim插件管理vundle备忘
  8. RelativeLayout的位置属性总结
  9. MVP ComCamp &amp; GCR MVP Openday 2014
  10. RMAN还原遭遇ORA-32006&amp;ORA-27102错误