Neo
Neo

Reputation: 5228

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 :)

Upvotes: 9

Views: 19267

Answers (3)

Padi Amu
Padi Amu

Reputation: 765

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

Upvotes: 0

Charles Hasegawa
Charles Hasegawa

Reputation: 199

I am using a more current release 2.4.3 of the hibernate-types-52 classes and was getting the same error.

I resolved it by using only the single typedef I needed on my entity class.

@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)

Postgres 9.4, Java 11, Hibernate 5.3.7

@Entity(name = "audit")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class Audit {
    @Type(type = "jsonb")
    @Column(name="audit_data", columnDefinition = "jsonb")
    private Map<String,Object> auditData;
...

Upvotes: 19

Ashok Parmar
Ashok Parmar

Reputation: 408

Try the below

public class ProjectPostgreSQL95Dialect extends PostgreSQL95Dialect{

public ProjectPostgreSQL95Dialect() {
    this.registerColumnType(Types.JAVA_OBJECT, "jsonb");
}

}

in application.properties file replace below.

hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect

replace

hibernate.dialect=com.ashok.config.PostgreSQL95Dialect

Upvotes: 0

Related Questions