Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 963
elcharrua Avatar asked Dec 07 '25 11:12

elcharrua


1 Answers

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


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!