简体   繁体   中英

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

I'm using jsonb in springboot(2.1)+postgres(10.5)+hibernate(5.3.7).

Following are changes in file:

  1. In pom.xml

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

  1. Entity definition:

```

@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
}

```

On running springboot application it given below error: nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [jsonb]

All other settings are standard sprintboot settings working with postgres. Post creation of this above error was coming.

Please let me know possible reason for same, Thanks in advance :)

I'm using jsonb in springboot(2.1)+postgres(10.5)+hibernate(5.3.7).

Following are changes in file:

  1. In pom.xml

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

  1. Entity definition:

```

@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
}

```

On running springboot application it given below error: nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [jsonb]

All other settings are standard sprintboot settings working with postgres. Post creation of this above error was coming.

Please let me know possible reason for same, Thanks in advance :)

I'm using jsonb in springboot(2.1)+postgres(10.5)+hibernate(5.3.7).

Following are changes in file:

  1. In pom.xml

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

  1. Entity definition:

```

@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
}

```

On running springboot application it given below error: nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [jsonb]

All other settings are standard sprintboot settings working with postgres. Post creation of this above error was coming.

Please let me know possible reason for same, Thanks in advance :)

what I will recommend is :

create a class that registers that datatype in your dialect

// remember I'm assuming you are using Postgres DB. Replace the

extends PostgreSQL94Dialect

with the dialect of your DB.

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

next in your application.properties configure your app's spring.jpa.properties.hibernate.dialect

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

after the above has been completed Make sure every entity that will use the jsonb defined in your Dialect is declared on those entities will now understand the type definition

@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>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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