Search in sources :

Example 1 with AutoUpdateQuery

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

the class UpdateCommandTest method testExecute_throwsOptimisticLockException.

@Test
public void testExecute_throwsOptimisticLockException() throws Exception {
    Emp emp = new Emp();
    emp.setId(10);
    emp.setName("aaa");
    emp.setVersion(100);
    runtimeConfig.dataSource.connection.preparedStatement.updatedRows = 0;
    AutoUpdateQuery<Emp> query = new AutoUpdateQuery<>(_Emp.getSingletonInternal());
    query.setMethod(getClass().getDeclaredMethod(method.getName()));
    query.setConfig(runtimeConfig);
    query.setEntity(emp);
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    UpdateCommand command = new UpdateCommand(query);
    try {
        command.execute();
        fail();
    } catch (OptimisticLockException expected) {
    }
}
Also used : Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) AutoUpdateQuery(org.seasar.doma.jdbc.query.AutoUpdateQuery) Test(org.junit.jupiter.api.Test)

Example 2 with AutoUpdateQuery

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

the class UpdateCommandTest method testExecute_OriginalStates.

@Test
public void testExecute_OriginalStates() throws Exception {
    Emp emp = new Emp();
    emp.setId(1);
    emp.setName("hoge");
    emp.setVersion(10);
    Emp states = new Emp();
    states.setId(1);
    states.setName("foo");
    states.setVersion(10);
    emp.originalStates = states;
    AutoUpdateQuery<Emp> query = new AutoUpdateQuery<>(_Emp.getSingletonInternal());
    query.setMethod(getClass().getDeclaredMethod(method.getName()));
    query.setConfig(runtimeConfig);
    query.setEntity(emp);
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    int rows = new UpdateCommand(query).execute();
    query.complete();
    assertEquals(1, rows);
    String sql = runtimeConfig.dataSource.connection.preparedStatement.sql;
    assertEquals("update EMP set NAME = ?, VERSION = ? + 1 where ID = ? and VERSION = ?", sql);
    List<BindValue> bindValues = runtimeConfig.dataSource.connection.preparedStatement.bindValues;
    assertEquals(4, bindValues.size());
    assertEquals("hoge", bindValues.get(0).getValue());
    assertEquals(10, bindValues.get(1).getValue());
    assertEquals(1, bindValues.get(2).getValue());
    assertEquals(10, bindValues.get(3).getValue());
    Emp updatedStates = emp.originalStates;
    assertEquals(1, updatedStates.getId());
    assertEquals("hoge", updatedStates.getName());
    assertNull(updatedStates.getSalary());
    assertEquals(11, updatedStates.getVersion());
}
Also used : Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) AutoUpdateQuery(org.seasar.doma.jdbc.query.AutoUpdateQuery) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) Test(org.junit.jupiter.api.Test)

Example 3 with AutoUpdateQuery

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

the class UpdateCommandTest method testExecute.

@Test
public void testExecute() throws Exception {
    Emp emp = new Emp();
    emp.setId(1);
    emp.setName("hoge");
    emp.setVersion(10);
    emp.originalStates = new Emp();
    AutoUpdateQuery<Emp> query = new AutoUpdateQuery<>(_Emp.getSingletonInternal());
    query.setMethod(getClass().getDeclaredMethod(method.getName()));
    query.setConfig(runtimeConfig);
    query.setEntity(emp);
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    int rows = new UpdateCommand(query).execute();
    query.complete();
    assertEquals(1, rows);
    String sql = runtimeConfig.dataSource.connection.preparedStatement.sql;
    assertEquals("update EMP set NAME = ?, VERSION = ? + 1 where ID = ? and VERSION = ?", sql);
    List<BindValue> bindValues = runtimeConfig.dataSource.connection.preparedStatement.bindValues;
    assertEquals(4, bindValues.size());
    assertEquals("hoge", bindValues.get(0).getValue());
    assertEquals(10, bindValues.get(1).getValue());
    assertEquals(1, bindValues.get(2).getValue());
    assertEquals(10, bindValues.get(3).getValue());
}
Also used : Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) AutoUpdateQuery(org.seasar.doma.jdbc.query.AutoUpdateQuery) BindValue(org.seasar.doma.internal.jdbc.mock.BindValue) Test(org.junit.jupiter.api.Test)

Example 4 with AutoUpdateQuery

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

the class UpdateCommandTest method testExecute_suppressesOptimisticLockException.

@Test
public void testExecute_suppressesOptimisticLockException() throws Exception {
    Emp emp = new Emp();
    emp.setId(10);
    emp.setName("aaa");
    emp.setVersion(100);
    runtimeConfig.dataSource.connection.preparedStatement.updatedRows = 0;
    AutoUpdateQuery<Emp> query = new AutoUpdateQuery<>(_Emp.getSingletonInternal());
    query.setMethod(getClass().getDeclaredMethod(method.getName()));
    query.setConfig(runtimeConfig);
    query.setEntity(emp);
    query.setOptimisticLockExceptionSuppressed(true);
    query.setCallerClassName("aaa");
    query.setCallerMethodName("bbb");
    query.setSqlLogType(SqlLogType.FORMATTED);
    query.prepare();
    new UpdateCommand(query).execute();
    query.complete();
}
Also used : Emp(example.entity.Emp) example.entity._Emp(example.entity._Emp) AutoUpdateQuery(org.seasar.doma.jdbc.query.AutoUpdateQuery) Test(org.junit.jupiter.api.Test)

Aggregations

Emp (example.entity.Emp)4 example.entity._Emp (example.entity._Emp)4 Test (org.junit.jupiter.api.Test)4 AutoUpdateQuery (org.seasar.doma.jdbc.query.AutoUpdateQuery)4 BindValue (org.seasar.doma.internal.jdbc.mock.BindValue)2 OptimisticLockException (org.seasar.doma.jdbc.OptimisticLockException)1