Home » Java » java – Spring Boot with JPA – wrong column type encountered in column-Exceptionshub

java – Spring Boot with JPA – wrong column type encountered in column-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m using Spring Boot Starter Data Jpa (2.1.9.RELEASE) and I’m trying to map this table:

CREATE TABLE `foo` (
  `id` int(11) NOT NULL,
  `woo` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

The Entity is mapped as follows:

@Entity
@Table(name = "foo")
public class Foo {

    @Id
    @Column(name = "id")
    private Integer id;

    @Column(name = "woo")
    private Integer woo;

    public Integer getId() {
        return id;
    }

    public Integer getWoo() {
        return woo;
    }

}

When I start the application I get the following error:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [woo] in table [foo]; found [tinyint (Types#TINYINT)], but expecting [integer (Types#INTEGER)]

My properties are:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///test
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=validate

I tried to do the same mapping using vanilla Jpa and Hibernate (not managed by Spring) and it works fine.

Can anyone point me in the right direction?

Here’s a link to the maven project, in case anyone want to try it directly.

How to&Answers:

You probably need to override the default JDBC type Hibernate using an explicit type declaration:

@Column(name = "woo", columnDefinition = "TINYINT(4)")
private Integer woo;

This should fix.

Answer:

Your database is using a TinyInt, while your class is making it expect an integer. TinyInt maps to byte however, which is where the confusion is taking place.

@Column(name = "woo")
private Byte woo;

A reference for mapping types can be found here.

Answer:

JDBC tinyint equivalent Java type is Byte.
Here is the hibernate documentation. Datatype mapping

JBoss tool will be helpful if you are using eclipse/STS to generate entities directly from the tables without datatype conflict.