gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:03:47 25 4
gpt4 key购买 nike

我正在使用 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)]

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

最佳答案

添加这个 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

解释见this article

关于java - Spring boot jpa/hibernate 遇到列类型错误(json字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53696987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com