繁体   English   中英

春靴 | JSONB Postgres | 异常:无法加载类 [jsonb]

[英]Springboot | JSONB Postgres | Exception: Unable to load class [jsonb]

我在 springboot(2.1)+postgres(10.5)+hibernate(5.3.7) 中使用 jsonb。

以下是文件中的更改:

  1. 在 pom.xml 中

.... <dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.3.5</version> </dependency> ....

  1. 实体定义:

``

@Entity(name = "Event")
@Table(name = "event")
@TypeDefs({
    @TypeDef(name = "string-array", typeClass = StringArrayType.class),
    @TypeDef(name = "int-array", typeClass = IntArrayType.class),
    @TypeDef(name = "json", typeClass = JsonStringType.class),
    @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
    @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
    @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class),
})
public class Event {

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private List<Location> alternativeLocations = new ArrayList<Location>();

    //Getters and setters omitted for brevity
}

``

在运行 springboot 应用程序时,它给出了以下错误: nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [jsonb]

所有其他设置都是使用 postgres 的标准 sprintboot 设置。 创建此上述错误后即将到来。

请让我知道相同的可能原因,提前致谢:)

我在 springboot(2.1)+postgres(10.5)+hibernate(5.3.7) 中使用 jsonb。

以下是文件中的更改:

  1. 在 pom.xml 中

.... <dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.3.5</version> </dependency> ....

  1. 实体定义:

``

@Entity(name = "Event")
@Table(name = "event")
@TypeDefs({
    @TypeDef(name = "string-array", typeClass = StringArrayType.class),
    @TypeDef(name = "int-array", typeClass = IntArrayType.class),
    @TypeDef(name = "json", typeClass = JsonStringType.class),
    @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
    @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
    @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class),
})
public class Event {

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private List<Location> alternativeLocations = new ArrayList<Location>();

    //Getters and setters omitted for brevity
}

``

在运行 springboot 应用程序时,它给出了以下错误: nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [jsonb]

所有其他设置都是使用 postgres 的标准 sprintboot 设置。 创建此上述错误后即将到来。

请让我知道可能的原因,提前致谢:)

我在 springboot(2.1)+postgres(10.5)+hibernate(5.3.7) 中使用 jsonb。

以下是文件中的更改:

  1. 在 pom.xml 中

.... <dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.3.5</version> </dependency> ....

  1. 实体定义:

``

@Entity(name = "Event")
@Table(name = "event")
@TypeDefs({
    @TypeDef(name = "string-array", typeClass = StringArrayType.class),
    @TypeDef(name = "int-array", typeClass = IntArrayType.class),
    @TypeDef(name = "json", typeClass = JsonStringType.class),
    @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
    @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
    @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class),
})
public class Event {

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private List<Location> alternativeLocations = new ArrayList<Location>();

    //Getters and setters omitted for brevity
}

``

在运行 springboot 应用程序时,它给出了以下错误: nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [jsonb]

所有其他设置都是使用 postgres 的标准 sprintboot 设置。 创建此上述错误后即将到来。

请让我知道可能的原因,提前致谢:)

我会推荐的是:

创建一个在您的方言中注册该数据类型的类

// 记住我假设您使用的是 Postgres DB。 更换

扩展PostgreSQL94方言

用你数据库的方言。

public class MyPostgreSQL94Dialect extends PostgreSQL94Dialect {
    public MyPostgreSQL94Dialect() {
        this.registerColumnType(Types.JAVA_OBJECT, "jsonb");
    }
}

接下来在您的 application.properties 配置您的应用程序的 spring.jpa.properties.hibernate.dialect

spring.jpa.properties.hibernate.dialect={java_path_to_this_class}.MyPostgreSQL94Dialect

完成上述操作后,确保将使用方言中定义的 jsonb 的每个实体都在这些实体上声明,现在将理解类型定义

@Data
@TypeDefs({@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)})
public class Event implements Serializable {

    @Id
    private String id;

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private List<Location> alternativeLocations = new ArrayList<Location>();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM