mybatis / mybatis/thymeleaf-scripting

Cannot use object properties correctly if SPRING_NAMED_PARAMETER

Open
#216 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
24
Forks
6
Avg merge
2d 23h
Merged PRs (30d)
6

Description

I using below version:

  1. org.mybatis.scripting:mybatis-thymeleaf:1.0.4
  2. org.springframework:spring-jdbc:6.0.9
  3. java 17

This issue occurs when using object properties with mb:p.

Probably because Spring JDBC does not allow you to specify properties of objects.

Exception thrown

java.lang.RuntimeException: org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter 'person.id': No value registered for key 'person.id'

This issue can be avoided by using mb:bind="person.id=${person.id}".

However, it cannot be used inside th:each. (Correctly, it will be overwritten by the value from the last loop.)

This causes the "Bulk insert" section of the document to fail.

The code is as follows.

Domain objects

public class Person {

        private String id;
        private String name;
        private String birthDate;

        public String getId() {
            return id;
        }
        public String getName() {
            return name;
        }
        public String getBirthDate() {
            return birthDate;
        }
}

Sql Template

INSERT INTO
    
    person

    VALUES(
        /*[# mb:p="person.id"/]*/,
        /*[# mb:p="person.name"/]*/,
        /*[# mb:p="person.birthDate"/]*/
    )

;

Creating SqlGenerator Method

public SqlGenerator createSqlGenerator() {

        SqlGeneratorConfig config = SqlGeneratorConfig.newInstanceWithCustomizer(c ->
            c.getDialect().setBindVariableRenderInstance(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER)
        );

        return new SqlGenerator(config);
}

Run query Method

public int insert(String sqlTemplate, SqlGenerator gen, NamedParameterJdbcTemplate jdbc) {
        final Person person = new Person();
        person.id = "ID-001";
        person.name = "John Doe";
        person.birthDate = "2001-01-01"; 

        final Map<String, Object> params = new HashMap<>();
        params.put("person", person);

        System.out.println(params);

        final String jdbcSql = gen.generate(sqlTemplate, params, params::put);
        
        System.out.println(jdbcSql);
        System.out.println(params);

        return jdbc.update(jdbcSql, params);
}

Contributor guide

Open the contributing guide

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

Reproduce the failure through SqlGenerator with SPRING_NAMED_PARAMETER and NamedParameterJdbcTemplate, using the object-property parameters shown in the issue. Trace how mb:p names are registered and resolved, then verify that object properties work inside the documented Bulk insert loop without requiring mb:bind.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.