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

elc*_*rua 3 java mysql json hibernate spring-boot

我正在使用 Spring Boot 将表映射到 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)]
Run Code Online (Sandbox Code Playgroud)

linter_summarydb 中的字段是 JSON 类型,在我的 pojo 上是一个字符串。我不明白为什么会出现此错误,Java 中是否有用于 JSON 字段的特殊变量?

小智 8

添加此 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>
Run Code Online (Sandbox Code Playgroud)

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

@TypeDefs({
    @TypeDef(name = "json", typeClass = JsonStringType.class),
    @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
Run Code Online (Sandbox Code Playgroud)

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

@Type( type = "json" )
@Column( columnDefinition = "json" )
Run Code Online (Sandbox Code Playgroud)

这里@Typeorg.hibernate.annotations.Type

有关解释,请参阅这篇文章