spring-boot-jpa/hibernate列类型错误(json字段)

vbopmzt1  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(698)

我正在使用spring boot将表Map到pojo,出现以下错误:

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)]

田野 linter_summary 在db中是json类型,在我的pojo中是字符串。我不明白为什么会出现这样的错误,java中是否有一个特殊的变量用于json字段?

2uluyalo

2uluyalo1#

添加此maven依赖项:

<!-- https://mvnrepository.com/artifact/com.vladmihalcea/hibernate-types-52 -->
<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
    <version>${hibernate-types.version}</version>
</dependency>

接下来,将此注解添加到实体类:

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

然后将其添加到列定义中:

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

哪里 @Typeorg.hibernate.annotations.Type 有关解释,请参阅本文

相关问题