休眠批量插入 [英] Hibernate batch insert
问题描述
这是我在数据库中插入批处理的代码
This is my code to insert batch in database
@Override
public void addMultiple(){
session = get_session();
tx = session.beginTransaction();
for ( int i=0; i<100; i++ ) {
Person person = new Person();
person.setName("oll"+i);
session.save(person);
if( i % 20 == 0 ) { // Same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
}
这是我的hibernate.cfg.xml
文件
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.jdbc.batch_versioned_data">true</property>
<property name="hibernate.jdbc.batch_size">20</property>
<property name="hibernate.order_inserts">true</property>
<property name="hibernate.current_session_context_class">thread</property>
但是在插入操作之后,我可以看到查询是在控制台中打印的
but after insert operation i can see query is printed in console is
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
Hibernate: insert into Person (name) values (?)
这意味着它不会插入批处理.我的实体没有标识符生成.那是什么问题呢?为什么我不能插入批处理?
That means it doesn't insert batch. My entity has no identifier generation. So what is the problem? Why can't I insert batch?
我的实体类是
@Entity
public class Person {
@Id
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
推荐答案
很可能您实际上正在使用批处理,只是Hibernate为每个语句打印sql.
Very probably you are actually using batching, it is just that Hibernate prints sql for each statement.
要对此进行检查,请为org.hibernate
软件包启用DEBUG日志级别(如果要查看绑定变量,请为org.hibernate.type
启用TRACE级别),然后检查日志中是否显示以下内容:
To check this, enable DEBUG log level for org.hibernate
package (and TRACE level for org.hibernate.type
if you want to see bound variables), then check if following appears in the log:
-
重用批处理语句
Reusing batch statement
执行批量大小
如果为执行的批次大小打印了大于1的数字,则说明您正在使用批次.
If a number larger than 1 is printed for executed batch size, then you are using batching.
特定于MySQL,要确保MySQL驱动程序重写插入语句,请按照
Specific to MySQL, to make sure that MySQL driver rewrites insert statements, enable profileSQL
parameter in connection url as described here.
注意::JDBC批处理是 (如果已使用IDENTITY id生成器).
NOTE: JDBC batching is disabled if IDENTITY id generator is used.
我还建议您考虑将hibernate.order_updates
设置为true,以改善更新语句的批处理. 此处.
I also suggest that you consider setting hibernate.order_updates
to true, to improve batching of update statements as well. A nice summary about this Hibernate tuning is described here.
这篇关于休眠批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!