Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 515
Neo Avatar asked Dec 27 '18 10:12

Neo


People also ask

What is Jsonb in Postgres?

JSONB stands for “JSON Binary” or “JSON better” depending on whom you ask. It is a decomposed binary format to store JSON. JSONB supports indexing the JSON data, and is very efficient at parsing and querying the JSON data. In most cases, when you work with JSON in PostgreSQL, you should be using JSONB.

Does hibernate support Jsonb?

Hibernate's PostgreSQL dialect does not support the JSONB datatype, and you need to register it.


1 Answers

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;
...
like image 79
user1742058 Avatar answered Oct 30 '22 12:10

user1742058