Multi-row insert
Annotate the DAO method with @MultiInsert to execute a multi-row insert.
@Dao
public interface EmployeeDao {
@MultiInsert
int insert(List<Employee> employees);
@MultiInsert
MultiResult<ImmutableEmployee> insert(List<ImmutableEmployee> employees);
}
By using multi-row insert, you can issue SQL statements such as the following:
insert into EMPLOYEE (EMPLOYEE_ID, EMPLOYEE_NO, EMPLOYEE_NAME, AGE, VERSION)
values (?, ?, ?, ?, ?), (?, ?, ?, ?, ?)
The preInsert method of entity listener is called each entity when before executing insert if the entity listener is specified at Entity classes parameter.
Also the postInsert method of entity listener method is called each entity when after executing insert.
Note
The databases that support this feature are:
H2
MySQL
PostgreSQL
SQL Server
However, in the case of SQL Server, this feature cannot be executed on tables with an auto-increment primary key.
Return type
If the type argument of the Iterable parameter is an immutable entity class, the return type must be org.seasar.doma.jdbc.MultiResult with that entity class as an element.
If the type argument of the Iterable parameter is a mutable entity class, the return type must be int that represents updated count.
Parameter type
The parameter type must be a subtype of java.lang.Iterable that has the entity class as its element.
The parameter must not be null.
Automatically generated values
During the execution of a multi-insert, automatically generated values will be set to the entity properties.
Identifier
See Id generation.
Version number
If the application does not set a value to the version property or sets a value less than 0, the value 1 will be ultimately set to that property.
If the application explicitly sets a value greater than 0 to the version property, automatic generation will not occur.
See also Version.
Properties of @MultiInsert
exclude
Entity properties specified in the exclude property of @MultiInsert will be excluded from the insert targets, even if they are set as insertable in the
@Column annotation.
@MultiInsert(exclude = {"name", "salary"})
int insert(List<Employee> employees);
include
Only the entity properties specified in the include property of @MultiInsert will be included in the insert targets.
If the same entity property is specified in both the exclude and include properties, that entity property will not be included in the insert targets.
Entity properties with insertable set to false in the @Column annotation will not be included in the insert targets, even if they are specified in the include property.
@MultiInsert(include = {"name", "salary"})
int insert(List<Employee> employees);
Unique constraint violation
org.seasar.doma.jdbc.UniqueConstraintException is thrown if unique constraint violation is occurred.
Query timeout
You can specify seconds of query timeout to queryTimeout property within @MultiInsert annotation.
@MultiInsert(queryTimeout = 10)
int insert(List<Employee> employees);
Query timeout that is specified in config class is used if queryTimeout property is not set value.
SQL log output format
You can specify SQL log output format to sqlLog property within @MultiInsert annotation.
@MultiInsert(sqlLog = SqlLogType.RAW)
int insert(List<Employee> employees);
SqlLogType.RAW represent outputting log that is sql with a binding parameter.