Search in sources :

Example 1 with AutoBatchUpdateQuery

use of org.seasar.doma.jdbc.query.AutoBatchUpdateQuery in project doma by domaframework.

the class BatchUpdateCommandTest method testExecute_suppressesOptimisticLockException.

@Test
public void testExecute_suppressesOptimisticLockException() {
    Emp emp = new Emp();
    emp.setId(1);
    emp.setName("hoge");
    emp.setVersion(10);
    runtimeConfig.dataSource.connection.preparedStatement.updatedRows = 0;
    AutoBatchUpdateQuery<Emp> query = new AutoBatchUpdateQuery<>(_Emp.getSingletonInternal());
    query.setMethod(method);
    query.setConfig(runtimeConfig);
    query.setEntities(Collections.singletonList(emp));
    query.setOptimisticLockExceptionSuppressed(true);
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    new BatchUpdateCommand(query).execute();
    query.complete();
}
Also used : AutoBatchUpdateQuery(org.seasar.doma.jdbc.query.AutoBatchUpdateQuery) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) Test(org.junit.jupiter.api.Test)

Example 2 with AutoBatchUpdateQuery

use of org.seasar.doma.jdbc.query.AutoBatchUpdateQuery in project doma by domaframework.

the class BatchUpdateCommandTest method testExecute.

@Test
public void testExecute() {
    Emp emp1 = new Emp();
    emp1.setId(1);
    emp1.setName("hoge");
    emp1.setVersion(10);
    Emp emp2 = new Emp();
    emp2.setId(2);
    emp2.setName("foo");
    emp2.setVersion(20);
    AutoBatchUpdateQuery<Emp> query = new AutoBatchUpdateQuery<>(_Emp.getSingletonInternal());
    query.setMethod(method);
    query.setConfig(runtimeConfig);
    query.setEntities(Arrays.asList(emp1, emp2));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    int[] rows = new BatchUpdateCommand(query).execute();
    query.complete();
    assertEquals(2, rows.length);
    String sql = runtimeConfig.dataSource.connection.preparedStatement.sql;
    assertEquals("update EMP set NAME = ?, SALARY = ?, VERSION = ? + 1 where ID = ? and VERSION = ?", sql);
    assertEquals(new Integer(11), emp1.getVersion());
    assertEquals(new Integer(21), emp2.getVersion());
}
Also used : AutoBatchUpdateQuery(org.seasar.doma.jdbc.query.AutoBatchUpdateQuery) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) Test(org.junit.jupiter.api.Test)

Example 3 with AutoBatchUpdateQuery

use of org.seasar.doma.jdbc.query.AutoBatchUpdateQuery in project doma by domaframework.

the class BatchUpdateCommandTest method testExecute_throwsOptimisticLockException.

@Test
public void testExecute_throwsOptimisticLockException() {
    Emp emp = new Emp();
    emp.setId(1);
    emp.setName("hoge");
    emp.setVersion(10);
    runtimeConfig.dataSource.connection.preparedStatement.updatedRows = 0;
    AutoBatchUpdateQuery<Emp> query = new AutoBatchUpdateQuery<>(_Emp.getSingletonInternal());
    query.setMethod(method);
    query.setConfig(runtimeConfig);
    query.setEntities(Collections.singletonList(emp));
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    BatchUpdateCommand command = new BatchUpdateCommand(query);
    try {
        command.execute();
        fail();
    } catch (OptimisticLockException expected) {
    }
}
Also used : AutoBatchUpdateQuery(org.seasar.doma.jdbc.query.AutoBatchUpdateQuery) Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) Test(org.junit.jupiter.api.Test)

Aggregations

Emp (example.entity.Emp)3 example.entity._Emp (example.entity._Emp)3 Test (org.junit.jupiter.api.Test)3 AutoBatchUpdateQuery (org.seasar.doma.jdbc.query.AutoBatchUpdateQuery)3 OptimisticLockException (org.seasar.doma.jdbc.OptimisticLockException)1