elcharrua
elcharrua

Reputation: 1682

Spring boot jpa / hibernate wrong column type encounter (json field)

I'm working on mapping a table into POJO using spring boot, and i'm getting the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/mercadolibre/linters/db/config/DbaConfig.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [linter_summary] in table [result]; found [json (Types#CHAR)], but expecting [varchar(255) (Types#VARCHAR)]

the field linter_summary in the db is of type JSON and on my pojo is a String. I'm not understanding why it's making this error, is there a special variable in java for JSON fields?

Upvotes: 3

Views: 10262

Answers (2)

user10639668
user10639668

Reputation:

Add this Maven dependency:

<!-- https://mvnrepository.com/artifact/io.hypersistence/hypersistence-utils-hibernate-55 -->
<dependency>
    <groupId>io.hypersistence</groupId>
    <artifactId>hypersistence-utils-hibernate-55</artifactId>
    <version>${hypersistence-utils.version}</version>
</dependency>

Next, add this annotation to the entity class:

@TypeDefs({
    @TypeDef(name = "json", typeClass = JsonStringType.class),
    @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})

Then add this to the column definition:

@Type( type = "json" )
@Column( columnDefinition = "json" )

where @Type is org.hibernate.annotations.Type

For explanations see this article

Upvotes: 9

Matthew Blackmon
Matthew Blackmon

Reputation: 1

In some cases if you're not building a web server container, try adding

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </dependency>

This helps to provide some of the serialization features that you may need.

Upvotes: 0

Related Questions