JPA and Spring Boot : integer from DB (H2) doesn’t get mapped to boolean as expected

I am refactoring part of an Spring Boot 2.3.1 application, in which previous developers have used Integer type (with values 0 or 1) instead of boolean a bit everywhere in the code.

But I am noticing something I didn't expect : I've also changed the field type in the entity class, and when loading it from the DB, I get an error :

[2021-05-04T22:27:36.036Z] [main] [24] [ERROR] Parameter value [0] did not match expected type [java.lang.Boolean (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [0] did not match expected type [java.lang.Boolean (n/a)]
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [0] did not match expected type [java.lang.Boolean (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [0] did not match expected type [java.lang.Boolean (n/a)]
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(
    at com.sun.proxy.$Proxy247.findByTicketIdAndInternalComment(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.base/java.lang.reflect.Method.invoke(
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(
    at com.sun.proxy.$Proxy162.findByTicketIdAndInternalComment(Unknown Source) 

This happens when running the existing tests, with H2.

the field created by Liquibase at test start up is :


my field used to be :

private Integer internalComment;

and it was working.

Now it's :

private Boolean internalComment;

and it's not working.

In the orm.xml, I have this simple mapping :

        <basic name="internalComment">
            <column name="INTERNAL_COMMENT" nullable="true" />

My repository class is :

public interface JpaCommentEntityRepository
    extends CrudRepository<TicketCommentEntity, Long>, JpaSpecificationExecutor<TicketCommentEntity>

I've tried several things (making the Java field boolean instead of Boolean, declaring the column with various types like NUMBER(1,0)), but to no avail...

The only thing that works is when I change back to Integer, and perform a manual mapping to boolean when creating my domain object from the entit class.. So at least, I am sure this is the field causing problem, and not something else.

So in a way, my refactoring is already improving the code : the only place where the field is not a boolean is in the entity class.. but it forces be to add extra code for the mapping... and this should work directly, right ?

So I guess I am missing something, but I am running out of ideas on how to investigate / fix this.

Any help would be much appreciated, thanks !

Read more here:

Content Attribution

This content was originally published by Vincent F at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: