简体   繁体   中英

JPA/Hibernate Bulk/Batch Insert using Mysql with Database Generated ID's

Okay, I've searched forever and I can't seem to find a good way of accomplishing batch inserts with JPA/Hibernate and MySql.

I want to be able to save/insert many records at once using JPA, but by default batching behavior is disabled if you use GenerationType.IDENTITY . I'm aware that you can switch to GenerationType.SEQUENCE , but that isn't available on MySql and creating new tables and using GenerationType.TABLE is not an option in my scenario.

So in the end, I need an efficient way of doing batch/bulk inserts using JPA/Hibernate, MySQL, and database generated IDs. I know it's possible to do this efficiently because I can do it with a JDBC connection, but I'd really like to not have to write my own JDBC queries for each of my repositories.

Anyone know how to accomplish this?

I'm okay if I'm unable to get the updated entities with the IDs back (think void saveAll() instead of List<User> saveAll() ). My main requirement is this happens in one/two big queries instead of saving iteratively each entity like it does now when I call saveAll .


I can include more if needed, but my entity looks like this:

@Entity
@Builder
@Getter
@Setter
@With
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false, exclude = "id")
@Table(name = "user")
@ToString(callSuper = true, onlyExplicitlyIncluded = true)
public class User {

    @Id
    @ToString.Include
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "uID")
    private long id;

    private String name;
}

There is no way to accomplish JDBC batching on insert with Hibernate when using the identity generation strategy, because for Hibernate, every entity must have a PK value assigned after a persist/insert.

You can use Hibernate SPIs to implement this yourself though. Take a look at how Hibernate implements inserts here org.hibernate.persister.entity.AbstractEntityPersister#insert(java.lang.Object, java.lang.Object[], java.lang.Object, org.hibernate.engine.spi.SharedSessionContractImplementor) . You can reduce the complexity if you want to implement this only for a few known entities that only use a handful of features.

IDENTITY generator disables JDBC batch insert of hibernate and JPA. since the sequence is not supported in MySQL, there is no way to bulk/batch insert records using MySQL and spring data JPA. Please read my blog on that. This is not the end of the road. we can use the JDBC template or query DSL-SQL. To see how to implement using query DSL-SQL click here . For the JDBC template click here .

If you need type-safe, easy to code choose query DSL-SQL else choose JDBC template

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM