MyBatis version

3.5.1

Database vendor and version

H2

Test case or example project

Test (fresh DB, empty table)

tagDAO.failedInsertTags(new HashSet(Arrays.asList("tag1", "tag2")));

DAO.java

@Repository
class AuditTagRDBMSStore
{

    public void failedInsertTags(Set<String> tagList)
    {
        AuditEventMapper mapper = SQLTransactionTL.getSql().getMapper(AuditEventMapper.class);
        mapper.createTags(tagList);
        invalidateCache();
    }
}

AuditEventMapper.java:

public interface AuditEventMapper extends BasicCRUDMapper<AuditEventBean>
{
...
    void createTags(@Param("tagList") Set<String> tagList);
...
}

AuditEvent.xml

<insert id="createTags" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO AUDIT_TAGS (TAG) VALUES
        <foreach collection="tagList" item="item" index="index" separator=",">
            (#{item})
        </foreach>
    </insert>

CreateTable.xml

CREATE TABLE AUDIT_TAGS (
        ID INTEGER PRIMARY KEY AUTO_INCREMENT,
        TAG VARCHAR(200),
        UNIQUE(TAG)
        );

Steps to reproduce

Run test.

Expected result

This scenario is working well in version 3.4.6. Stop to work in 3.5.1.

Actual result

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id' in 'java.lang.String'.
### The error may exist in pl/edu/icm/unity/store/rdbms/mapper/AuditEvent.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: INSERT INTO AUDIT_TAGS (TAG) VALUES         (?)    ,     (?)
### Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id' in 'java.lang.String'.
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ~[mybatis-3.5.1.jar:3.5.1]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199) ~[mybatis-3.5.1.jar:3.5.1]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184) ~[mybatis-3.5.1.jar:3.5.1]
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62) ~[mybatis-3.5.1.jar:3.5.1]
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) ~[mybatis-3.5.1.jar:3.5.1]
    at com.sun.proxy.$Proxy86.createTags(Unknown Source) ~[?:?]
    at pl.edu.icm.unity.store.impl.audit.AuditTagRDBMSStore.failedInsertTags(AuditTagRDBMSStore.java:62) ~[classes/:?]

Comment From: harawata

Hi @remigiuszl ,

useGeneratedKeys="true" commands MyBatis to set the generated keys to the parameter objects. Please see the example in the doc.

In 3.4.6, it was just silently ignored (you didn't get the generated keys, right?), but throwing exception is the expected behavior. Removing useGeneratedKeys and keyProperty from the <insert /> element should resolve the error. Please let me know if it didn't work!

Comment From: remigiuszl

Thank you very much for quick response. Removing useGeenratedKeysand keyProperty solved the issue.

Hard to tell how the keys get generated in my case in 3.4.6 but the behavior is exactly as before.

Problem with my configuration - issue to be closed.

Comment From: harawata

@Alceatraz ,

The error reported in this issue occurred because the specified keyProperty did not exist. It was a misconfiguration and should have been an error, but was silently ignored in older versions.

How to get the inserted primary auto increase id ?

Just like it is documented. If you have any difficulty, please post your question to the mailing list or Stack Overflow. You should include basic information like versions of DB, driver, MyBatis; 'create table' statement; mapper statement; Java code; ... to get useful answers.