mybatis / mybatis/velocity-scripting

#set inside #repeat does not work correctly

Open
#14 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
49
Forks
36
Avg merge
3d 13h
Merged PRs (30d)
6

Description

Mapper:

<update id="velocity-nested-set" lang="velocity">
    #repeat( $_parameter.list $level1)
        #set( $foo = $level1)
        @{foo}
    #end
</update>

Test:

@Test
public void velocityTestNestedSet() throws SQLException, IOException {
    expect(connection.getAutoCommit()).andStubReturn(false);
    expect(connection.prepareStatement("\n" +
            "                    ?\n" +
            "                    ?\n" +
            "            ")).andReturn(statement);
    statement.setInt(1, 1);
    statement.addBatch();
    statement.setInt(2, 2);
    statement.addBatch();
    expect(statement.executeBatch()).andStubReturn(new int[]{2});
    statement.close();
    connection.setAutoCommit(true);
    connection.rollback();
    connection.close();

    replay();

    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(
            Resources.getResourceAsStream("configuration.xml"));
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, connection);
    List<Integer> param = Arrays.asList(1,2);
    sqlSession.insert("velocity-nested-set", param);
    sqlSession.flushStatements();
    sqlSession.close();

}

Exception:

java.lang.AssertionError: 
  Unexpected method call setInt(1, 2):
    setInt(1, 1): expected: 1, actual: 0
    addBatch(): expected: 2, actual: 0
    setInt(2, 2): expected: 1, actual: 0
    close(): expected: 1, actual: 0
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:56)
    at com.sun.proxy.$Proxy7.setInt(Unknown Source)
    at org.apache.ibatis.type.IntegerTypeHandler.setNonNullParameter(IntegerTypeHandler.java:28)
    at org.apache.ibatis.type.IntegerTypeHandler.setNonNullParameter(IntegerTypeHandler.java:23)
    at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:46)
    at org.apache.ibatis.type.UnknownTypeHandler.setNonNullParameter(UnknownTypeHandler.java:42)
    at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:46)
    at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:77)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:77)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:58)
    at org.apache.ibatis.executor.BatchExecutor.doUpdate(BatchExecutor.java:68)
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:100)
    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:148)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:137)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the mapper snippet using nested #repeat and #set, then run the velocityTestNestedSet test to reproduce the parameter-binding failure. Trace how the nested directives produce batch parameters; done means the test binds 1 and 2 at the expected positions and passes without the EasyMock assertion.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.