Leo
Leo

Reputation: 115

How to solve "Mapper method has an unsupported return type"

I'm getting the following error:

Mapper method 'nutri.api.infrastructure.datasource.client.ClientMapper.saveClient' has an unsupported return type: class nutri.api.domain.model.Client

I'm using Java 15, PostgreSQL, and MyBatis for my project.

I only understand that something is wrong with my Client.java and ClinetMapper.java but not really sure what's causing this error. Any suggestion would help a lot.

Here is my code

Client.java

import java.util.UUID;

public class Client {

    int id;
    String clientNumber;
    String name;
    String email;
    String healthCondition;


    public Client(int id, String clientNumber, String name, String email, String healthCondition) {
        this.id = id;
        this.clientNumber = UUID.randomUUID().toString();
        this.name = name;
        this.email = email;
        this.healthCondition = healthCondition;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getClientNumber() {
        return clientNumber;
    }

    public void setClientNumber(String clientNumber) {
        this.clientNumber = clientNumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getHealthCondition() {
        return healthCondition;
    }

    public void setHealthCondition(String healthCondition) {
        this.healthCondition = healthCondition;
    }

}

ClientMapper.java

@Options(useGeneratedKeys=true, keyProperty="id") Client saveClient(Client client);

ClientMapper.xml(MyBatis file)

<mapper namespace="nutri.api.infrastructure.datasource.client.ClientMapper">

<insert id="saveClient" parameterType="nutri.api.domain.model.Client">
        INSERT INTO client.data (
            id,
            client_number,
            name,
            email,
            health_condition
        ) VALUES (
            nextval('client.client_id_seq'),
            #{clientNumber},
            #{name},
            #{email},
            #{healthCondition}
        )
    </insert>

Upvotes: 2

Views: 5467

Answers (1)

ave
ave

Reputation: 3594

For insert statement, the method return type must be void or int.
If you use int, the number of inserted rows (always 1 in your case) will be returned.
The generated key will be set to the id field of the parameter instance.
Also, you should specify keyColumn.

@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
int saveClient(Client client);
Client param = ... // prepare parameter
int count = clientMapper.saveClient(param);
// param.getId() will return the generated key

Upvotes: 2

Related Questions