postgresqlmybatismybatis-mapper

Insert Java boolean into Postgresql boolean via Mybatis


I fail to insert a java boolean into a postgresql boolean column via mybatis. I get the below error. Why does it think 'expression is of type integer'? It's all boolean.

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: org.postgresql.util.PSQLException: ERROR: column columnname1 is of type boolean but expression is of type integer
  Hint: You will need to rewrite or cast the expression.
  Position: 880
  Location: File: parse_target.c, Routine: transformAssignedExpr, Line: 586
  Server SQLState: 42804
### The error may exist in URL [jar:file:/path1/jarname1.jar!/BOOT-INF/lib/jarname2.jar!/config/mybatis/mappername1.xml]
### The error may involve com.pathname1.pathname2.repository.mappername1.insert-Inline
### The error occurred while setting parameters
### SQL: insert into tablename1(             columnname1,             columnname2,             ...) values (             ?,             ?,...        )

I already researched a lot and rewrote the mapper in several ways, but the error remains the same.

This is the POJO:

package com.pathname1.pathname2.db.data.mybatis;

import ...

public class pojoname1{
    private ...
    private boolean pojofield1= false;
    private ...

    ...
    public boolean ispojofield1() {
        return pojofield1;
    }

    public void setpojofield1(boolean pojofield1) {
        this.pojofield1= pojofield1;
    }
    ...
}

This is the mapper (the javaType=boolean is one of the tries to get it to work, does not work without it either):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.pathname1.pathname2.repository.mappername1">

...

    <insert id="insert" parameterType="com.pathname1.pathname2.db.data.mybatis.pojoname1" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into tablename1(
            columnname1,
            ...
        ) values (
            #{pojofield1,jdbcType=BOOLEAN,javaType=boolean},
            ...
        )
    </insert>
    
...

</mapper>

Update:

Versions used: Java 8 mybatis-spring 2.1.2 mybatis 3.5.16 postgresql 42.7.3


Solution

  • A setParameter function in a Typehander with a MappedTypes annotation for boolean @MappedTypes({boolean.class, Boolean.class}) that used preparedStatement.setInt has caused the problem.