JPA / Hibernate批量(批量)插入 [英] JPA/Hibernate bulk(batch) insert

查看:1141
本文介绍了JPA / Hibernate批量(批量)插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在阅读关于jpa批量插入的几个主题后创建的简单示例,我有2个持久对象User和Site。一个用户可以有很多网站,所以我们在这里有一对多的关系。假设我想创建用户并创建/链接几个站点到用户帐户。这里是代码的样子,考虑到我愿意使用批量插入的Site对象。

  User user = new User(John多伊); 

user.getSites()。add(新网站(google.com,用户));
user.getSites()。add(新网站(yahoo.com,用户));

EntityTransaction tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(user);
tx.commit();

但是当我运行这段代码时(我使用hibernate作为jpa实现提供程序)输出:

  Hibernate:插入User(id,name)values(null,?)
Hibernate:call identity (),
Hibernate:插入站点(id,url,user_id)值(空,?,?)
Hibernate:调用identity()
Hibernate: user_id)values(null,?,?)
Hibernate:call identity()

,我的意思是真正的批量插入不起作用,或者我很困惑?

以下是源代码对于这个示例项目,这是maven项目,因此您只能下载并运行mvn install来检查输出。



更新:



在Ken Liu提供建议之后,我禁用了Site对象ID自动生成功能:

 用户用户=新用户(John Doe); 
user.getSites()。add(新网站(1,google.com,user));
user.getSites()。add(新网站(2,yahoo.com,用户));
entityManager.setFlushMode(FlushModeType.COMMIT);
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(user);
tx.commit();

现在我在调试输出中有以下行:



DEBUG:org.hibernate.jdbc.AbstractBatcher - 执行批量大小:2



它可以工作!

解决方案

如果您使用数据库来生成id,那么Hibernate必须执行查询以为每个实体生成主键。


Here is simple example I've created after reading several topics about jpa bulk inserts, I have 2 persistent objects User, and Site. One user could have many site, so we have one to many relations here. Suppose I want to create user and create/link several sites to user account. Here is how code looks like, considering my willing to use bulk insert for Site objects.

User user = new User("John Doe");

user.getSites().add(new Site("google.com", user));
user.getSites().add(new Site("yahoo.com", user));

EntityTransaction tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(user);
tx.commit();

But when I run this code (I'm using hibernate as jpa implementation provider) I see following sql output:

Hibernate: insert into User (id, name) values (null, ?)
Hibernate: call identity()
Hibernate: insert into Site (id, url, user_id) values (null, ?, ?)
Hibernate: call identity()
Hibernate: insert into Site (id, url, user_id) values (null, ?, ?)
Hibernate: call identity()

So, I means "real" bulk insert not works or I am confused?

Here is source code for this example project, this is maven project so you have only download and run mvn install to check output.

UPDATED:

After Ken Liu kindly advise, I've disabled Site object id auto generation:

    User user = new User("John Doe");
    user.getSites().add(new Site(1, "google.com", user));
    user.getSites().add(new Site(2, "yahoo.com", user));
    entityManager.setFlushMode(FlushModeType.COMMIT);
    EntityTransaction tx = entityManager.getTransaction();
    tx.begin();
    entityManager.persist(user);
    tx.commit();

Now I have following line in debug output:

DEBUG: org.hibernate.jdbc.AbstractBatcher - Executing batch size: 2

It works!

解决方案

If you're using the database to generate ids, then Hibernate has to execute a query to generate the primary key for each entity.

这篇关于JPA / Hibernate批量(批量)插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆