Why can't hibernate do batch inserts when I using IDENTITY generators (mysql8)


this is my configuration ffiles

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/study?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    hikari:
      auto-commit: false
      username: root
      password: asdfjj
  jpa:
    generate-ddl: false
    properties:
      hibernate:
        jdbc:
          batch_size: 100
          order_inserts: true

this is my code

  ArrayList<AdminUser> adminUsers = new ArrayList<>();
  for (int i = 0; i < 10; i++) {
      AdminUser adminUser = new AdminUser();
      adminUser.setLoginName("qin").setPassword("asdfjj").setRoleId(1L).setDeptId(1L);
      adminUsers.add(adminUser);
  }

  adminUserRepository.saveAll(adminUsers);

when I use jdbc, It works very well

        String url = "jdbc:mysql://127.0.0.1:3306/study?rewriteBatchedStatements=true";
        String user = "root";
        String password = "asdfjj";
        Connection connection = DriverManager.getConnection(url, user, password);
        long start = System.currentTimeMillis();
        System.out.println(start);
        final String login_name = "qin";
        final String login_password = "asdfjj";

        PreparedStatement preparedStatement = connection
                .prepareStatement("INSERT INTO admin_user (login_name, password, dept_id, role_id) VALUES (?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
        preparedStatement.setString(1, login_name);
        preparedStatement.setString(2, login_password);
        preparedStatement.setInt(3, 1);
        preparedStatement.setInt(4, 1);
        preparedStatement.addBatch();
        preparedStatement.setString(1, login_name);
        preparedStatement.setString(2, login_password);
        preparedStatement.setInt(3, 1);
        preparedStatement.setInt(4, 1);
        preparedStatement.addBatch();
        preparedStatement.executeBatch();
        ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
        while (generatedKeys.next()) {
            long id = generatedKeys.getLong(1);
            System.out.println(id);
        }

jpa version: 3.3.2
hibernate version: 6.5.2

Hibernate can use identity generators for databases which supports it, the screenshot you posted only suggests to use sequence generators when possible to take full advantage of other functionality (e.g. batch inserts).

But mysql jdbc supports batch inserts, I don’t know why hibernate don’t support batch inserts.

As I already said, Hibernate only supports batch inserts with generated identifiers when using the sequence generator - using IDENTITY columns will disable this functionality. You have to understand the added functionality of an ORM framework comes with some limitations, and this is one of them.

JPA requires that EntityManager#persist sets the generated identifier on the entity, so Hibernate ORM has to do an insert when the IDENTITY strategy is used.
If Hibernate ORM had a Session#persistAll method, in theory, we could still do a batch insert if the JDBC driver gives access to all the generated identifiers, but I am not sure that the MySQL JDBC driver able to do that. If you can provide a JDBC only example that shows this works along with a link to documentation that supports this claim, we can look into adding such a method to allow insert batching in this case.