【发布时间】:2015-08-01 20:24:49
【问题描述】:
这是我在数据库中插入批处理的代码
@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>
但插入操作后我可以看到查询打印在控制台是
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 (?)
这意味着它不会插入批处理。我的实体没有标识符生成。那么问题是什么?为什么我不能插入批处理?
我的实体类是
@Entity
public class Person {
@Id
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
【问题讨论】:
-
请看我编辑的答案。您是否尝试使用
profileSQL参数检查 MySQL 驱动程序是否重写语句?