Hibernate批量处理数据

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();

更多详情请关注 http://www.cnblogs.com/baixingqiang/
原文地址:https://www.cnblogs.com/baixingqiang/p/5852720.html