扫码一下
查看教程更方便
考虑需要使用 hibernate 将大量记录上传到数据库中的情况。 以下是使用 hibernate 实现此目的的代码片段
session session = sessionfactory.opensession();
transaction tx = session.begintransaction();
for ( int i=0; i<100000; i ) {
employee employee = new employee(.....);
session.save(employee);
}
tx.commit();
session.close();
默认情况下,hibernate 会将所有持久化对象缓存在会话级缓存中,最终我们的应用程序将在第 50,000 行左右出现 outofmemoryexception
异常。 如果在 hibernate 中使用批处理,则可以解决此问题。
要使用批处理功能,首先将 hibernate.jdbc.batch_size
设置为批处理大小,根据对象大小设置为 20 或 50。 这将告诉休眠容器每 x 行作为批处理插入。 要在代码中实现这一点,我们需要做一些修改,如下所示
session session = sessionfactory.opensession();
transaction tx = session.begintransaction();
for ( int i=0; i<100000; i ) {
employee employee = new employee(.....);
session.save(employee);
if( i % 50 == 0 ) { // same as the jdbc batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
上面的代码对于 insert 操作可以正常工作,但是如果愿意进行 update 操作,那么可以使用以下代码来实现
session session = sessionfactory.opensession();
transaction tx = session.begintransaction();
scrollableresults employeecursor = session.createquery("from employee").scroll();
int count = 0;
while ( employeecursor.next() ) {
employee employee = (employee) employeecursor.get(0);
employee.updateemployee();
seession.update(employee);
if ( count % 50 == 0 ) {
session.flush();
session.clear();
}
}
tx.commit();
session.close();
让我们修改配置文件来添加 hibernate.jdbc.batch_size
属性
org.hibernate.dialect.mysqldialect
com.mysql.jdbc.driver
jdbc:mysql://localhost/hibernate_demo
root
123456
50
考虑以下 pojo employee 类
public class employee {
private int id;
private string firstname;
private string lastname;
private int salary;
public employee() {}
public employee(string fname, string lname, int salary) {
this.firstname = fname;
this.lastname = lname;
this.salary = salary;
}
public int getid() {
return id;
}
public void setid( int id ) {
this.id = id;
}
public string getfirstname() {
return firstname;
}
public void setfirstname( string first_name ) {
this.firstname = first_name;
}
public string getlastname() {
return lastname;
}
public void setlastname( string last_name ) {
this.lastname = last_name;
}
public int getsalary() {
return salary;
}
public void setsalary( int salary ) {
this.salary = salary;
}
}
让我们创建以下 employee 表来存储 employee 对象
create table employee (
id int not null auto_increment,
first_name varchar(20) default null,
last_name varchar(20) default null,
salary int default null,
primary key (id)
);
以下将是映射 employee 对象与 employee 表的映射文件
this class contains the employee detail.
最后,我们将使用 main()
方法创建我们的应用程序类来运行应用程序,我们将使用 session
对象可用的 flush()
和 clear()
方法,以便 hibernate 不断将这些记录写入数据库,而不是将它们缓存在内存中。
import java.util.*;
import org.hibernate.hibernateexception;
import org.hibernate.session;
import org.hibernate.transaction;
import org.hibernate.sessionfactory;
import org.hibernate.cfg.configuration;
public class manageemployee {
private static sessionfactory factory;
public static void main(string[] args) {
try {
factory = new configuration().configure().buildsessionfactory();
} catch (throwable ex) {
system.err.println("failed to create sessionfactory object." ex);
throw new exceptionininitializererror(ex);
}
manageemployee me = new manageemployee();
/* 批量添加员工记录 */
me.addemployees( );
}
/* 批量创建员工记录的方法 */
public void addemployees( ){
session session = factory.opensession();
transaction tx = null;
integer employeeid = null;
try {
tx = session.begintransaction();
for ( int i=0; i<100000; i ) {
string fname = "first name " i;
string lname = "last name " i;
integer salary = i;
employee employee = new employee(fname, lname, salary);
session.save(employee);
if( i % 50 == 0 ) {
session.flush();
session.clear();
}
}
tx.commit();
} catch (hibernateexception e) {
if (tx!=null) tx.rollback();
e.printstacktrace();
} finally {
session.close();
}
return ;
}
}
以下是编译和运行上述应用程序的步骤。 在继续编译和执行之前,请确保我们已正确设置 path 和 classpath。