首页 文章

Spring引导jpa / hibernate错列类型遭遇(json字段)

提问于
浏览
1

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

db中的字段 linter_summary 是JSON类型,而我的pojo上的字段是String . 我发生了这个错误,java中的JSON字段有一个特殊的变量吗?

1 回答

  • 1

    添加此Maven依赖项:

    <!-- https://mvnrepository.com/artifact/com.vladmihalcea/hibernate-types-52 -->
        <dependency>
            <groupId>com.vladmihalcea</groupId>
            <artifactId>hibernate-types-52</artifactId>
            <version>2.3.5</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

相关问题