domaframework / doma

DAO oriented database mapping framework for Java
https://doma.readthedocs.io/
Apache License 2.0
438 stars 69 forks source link

Support multi-row insert #1132

Closed nakamura-to closed 1 month ago

nakamura-to commented 1 month ago

This pull request supports multi-row insert.

Using multi-row insert, you can issue the following SQL:

insert into DEPARTMENT (DEPARTMENT_ID, DEPARTMENT_NO, DEPARTMENT_NAME, LOCATION, VERSION) 
values (99, 99, 'aaa', 'bbb', 1), (100, 100, 'ccc', 'ddd', 1)

The Dao definition can be written as follows:

@Dao
public interface DepartmentDao {
  @MultiInsert
  int insertMutableEntities(List<Department> entities);

  @MultiInsert
  MultiResult<ImmutableDepartment> insertImmutableEntities(List<ImmutableDepartment> entities);
}

In the Criteria API, you can execute a multi-row insert by calling the insertMulti method as follows:

Department_ d = new Department_();
List<Department> departments = Arrays.asList(department, department2);
MultiResult<Department> result = entityql.insertMulti(d, departments).execute();

The databases that support this feature are as follows:

However, in the case of SQL Server, this feature cannot be executed on tables with an auto-increment primary key.