Mybatis-3: ExecutorException: No setter found for the keyProperty 'id'

Created on 23 Jan 2017  ·  6Comments  ·  Source: mybatis/mybatis-3

MyBatis version

3.4.2

Database vendor and version

PostgreSQL 9.6.1

Test case or example project

interface TicketMapper {
    @Options(flushCache = Options.FlushCachePolicy.TRUE, useGeneratedKeys=true) // Set keyProperty as the Java variable name and keyColumn as the column name in the database.
    @Insert("INSERT INTO tickets (apikey, ticket) VALUES (#{apikey},#{ticket})")
    public void insertTicket(final TicketTO ticket) throws RecordAlreadyExists;
   }
-- Tickets für die WebSocket-Anmeldung
CREATE TABLE tickets (
  ID SERIAL PRIMARY KEY,
  crdate    TIMESTAMP DEFAULT LOCALTIMESTAMP,

    -- apikey - ist in diesem Fall ein WebToken
    apikey     VARCHAR(2048)    not null,

  -- das eigentliche Ticket, eine UUID
  ticket    VARCHAR(36) not null
);
public class TicketTO {

    /** Verweis auf den User */
    private          String   apikey;
    private          String   ticket;
    private DateTime crdate;

    public String getAppID() {  ...  }

    public String getTicket() { ...}

    public void setTicket(final String ticket) { ... }

    public DateTime getCrdate() { ... }

    public void setCrdate(final String crdate) { ... }
}
````

This line produces to above error:
```java
getSession().getMapper(TicketMapper.class).insertTicket(ticket);

It's true that TicketTO has no "id" but this should be valid!
It works with 3.4.1 but fails with 3.4.2

bug

Most helpful comment

This should be fixed in the latest 3.4.5-SNAPSHOT .
It would be great if you could try it and see if your problem is resolved.
Thanks!

All 6 comments

Could you please create a demo project including the configuration so that we can reproduce the issue quickly and reliably?
Here are some skeleton projects : https://github.com/harawata/mybatis-issues
Thank you!

Hi @MikeMitterer,

Probably, This issue related with #782.
I think this behavior is work as designed on 3.4.2.
If you want to avoid this behavior, I think useGeneratedKeys should be set to false (or omit this option).

I have a one question. Why did you set useGeneratedKeys=true in this case ?

782 explains it, it seems.

Please let us know if you think we are missing something!

We've got another report on the mailing list.
https://groups.google.com/d/msg/mybatis-user/H940lW-qlXQ/K3MAEL7mCQAJ

782 might cause problem on PostgreSQL.

Reopen to reevaluate.

I didn't know that, but there is a default value for keyProperty when using annotation (i.e. @Insert or @Update) and this causes a problem when useGeneratedKeys is enabled in the global config and the database is PostgreSQL [1].
Here are failing tests demonstrating the issue (it uses postgresql-embedded for portability).

Although it is not ideal, we have to revert #782 for now, I think.
The _right_ fix would be to remove the default value for keyProperty, but we must wait for the next major release because it could break backward compatibility.
Does anyone have a better idea?

[1] In PostgreSQL, ResultSetMetaData#getColumnCount() always returns the number of the columns of the inserted/updated even when there is no SERIAL column in the table.

This should be fixed in the latest 3.4.5-SNAPSHOT .
It would be great if you could try it and see if your problem is resolved.
Thanks!

Was this page helpful?
0 / 5 - 0 ratings