【发布时间】:2021-09-03 03:40:09
【问题描述】:
这是mysql表。
create table Customer
(
id int auto_increment primary key,
birth date null,
createdTime time null,
updateTime datetime(6) null
);
这是我的java代码
@Before
public void init() {
this.entityManagerFactory=Persistence.createEntityManagerFactory("jpaLearn");
this.entityManager=this.entityManagerFactory.createEntityManager();
this.entityTransaction=this.entityManager.getTransaction();
this.entityTransaction.begin();
}
@Test
public void persistentTest() {
this.entityManager.setFlushMode(FlushModeType.COMMIT); //don't work.
for (int i = 0; i < 1000; i++) {
Customer customer = new Customer();
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setUpdateTime(new Date());
this.entityManager.persist(customer);
}
}
@After
public void destroy(){
this.entityTransaction.commit();
this.entityManager.close();
this.entityManagerFactory.close();
}
当我阅读 JPA 的 wikibooks 时,它说“这意味着当您调用持久、合并或删除数据库时,不会执行 DML INSERT、UPDATE、DELETE,直到提交或触发刷新。” 但是在我的代码运行的同时,我阅读了mysql日志,我发现每次持久执行时,mysql都会执行sql。而且我还看了wireShark,每次都会引起对Database的请求。 我记得jpa saveAll方法可以批量发送SQL语句到数据库吗?如果要插入10000条记录,如何提高效率?
【问题讨论】:
标签: jpa