Skip to content
Advertisement

How to update a JSONB column in PostgreSQL using a Spring Data JPA Query

I have the following Controller

JavaScript

If i remove/comment

JavaScript

That is i don’t add the fetched result to the new updatedAdditionDetails (of type ArrayList), the DB updates successfully Otherwise i get the below error :

ERROR: column “itemsinventory_addtion_details” is of type jsonb but expression is of type recordn Hint: You will need to rewrite or cast the expression.n Position

In my Repository i have the following query

JavaScript

I have tried it the JPA way as follows:

JavaScript

This unfortunately has fetched the same error. Any help on this is greatly appreciated. Thanks in advance.

Advertisement

Answer

The cause of the error

The PostgreSQL error looks as follows:

ERROR: column “itemsinventory_addtion_details” is of type jsonb but expression is of type recordn Hint: You will need to rewrite or cast the expression.n Position

This error is caused because the List is passed like this:

JavaScript

By default, Spring Data JPA, which uses Hibernate, doesn’t know how to handle JSON types. So, you need to use a custom Hibernate Type, like the JsonBinaryType offered by the Hibernate Types project.

Maven dependency

First, you need to add the Hibernate Types dependency:

JavaScript

After you set the hibernate-types.version Maven property to the latest version, the dependency will be downloaded from Maven Central.

Setting the Hibernate Type explicitly

You won’t be able to use the Spring @Query annotation since you can’t set the Hibernate Type explicitly.

So, you need to add a custom Spring Data JPA Repository implementation with the following method:

JavaScript

That’s it!

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement