5

I am just trying to map a boolean value with Mybatis, but I am having a problem. Firstly, I'll show you the parts involved:

XML File:

<resultMap id="destinationTypeMap" type="DestinationTypeDTO">
        <result property="destinationTypeId" column="education_destination_type_id" javaType="java.lang.Long" jdbcType="NUMERIC"/>
        <result property="description" column="description" javaType="java.lang.String" jdbcType="VARCHAR"/>
        <result property="available" column="is_available" javaType="boolean" jdbcType="VARCHAR" typeHandler="BooleanHandler"/>
    </resultMap>

Java class:

public class DestinationTypeDTO {

    private long destinationTypeId;
    private String description;
    private boolean available;

    public long getDestinationTypeId() {
        return destinationTypeId;
    }

    public void setDestinationTypeId(long destinationTypeId) {
        this.destinationTypeId = destinationTypeId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

}

But, I am getting this error log:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'isAvailable' of '....DestinationTypeDTO@bbd76bf' with value 'true' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'isAvailable' in 'class ....DestinationTypeDTO'

I spent hours trying to find what's going on but without success. Any hint?

Thanks everyone.

stack man
  • 2,303
  • 9
  • 34
  • 54

4 Answers4

8

Change javaType="boolean" to java.lang.Boolean and specify property="available"

<result property="available" column="is_available" property="available" javaType="java.lang.Boolean" jdbcType="VARCHAR" typeHandler="BooleanHandler"/>

In your class change private boolean available; to private Boolean isAvailable; and add getter/setter

public void setIsAvailable(Boolean available) {
    this.available = available;
}

public Boolean getIsAvailable() {
    return available;
}
sol4me
  • 15,233
  • 5
  • 34
  • 34
4

Use

javaType="_boolean"

Not

javaType="boolean"

_boolean maps to boolean, boolean maps to java.lang.Boolean.

Documentation: http://www.mybatis.org/mybatis-3/configuration.html

Mateusz Stefek
  • 173
  • 1
  • 6
2

Change your setter , ibatis expects boolean name with standard format of pojo:-

 public void setIsAvailable(boolean available) {
    this.available = available;
}
Panther
  • 3,312
  • 9
  • 27
  • 50
  • I did it but still not working. I am still getting this: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'isAvailable' of 'uk.co.corelogic.mosaic.dto.education.DestinationTypeDTO@bbd76bf' with value 'true' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'isAvailable' in 'class uk.co.corelogic.mosaic.dto.education.DestinationTypeDTO' – stack man Dec 19 '14 at 15:23
0

Your database column is named "is_available" and your attribute is named "available" that's why the mapping is not working, you have two solutions for this:

  1. Change the name of the column to "available" or change the name of the attribute"is_available"(not ok for Java)

  2. In the sql query use "is_available as available".

cgab
  • 45
  • 1
  • 1
  • 7