Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql Jsonb Java Hibernate 6 JdbcTypeCode

To persist I use Hibernate 6 and use on the entity:

@JdbcTypeCode(SqlTypes.JSON)
private String value;

When you save to the database, save wrapped in quotes and inside the json escape the quotes:

"{\\\"name\\\":\\\"pepito\\\", \\\"lastname\\\":\\\"perez\\\"}"

when I use the query to select value -> 'name' does not work, I do not understand why it does not save in the database in the following way:

{"name":"pepito", "lastname":"perez"}

Thanks.

like image 396
Gabriel Avatar asked Apr 27 '26 22:04

Gabriel


2 Answers

Hibernate ORM 6.1.3.Final have fix for this issue, strings annotated with @JdbcTypeCode(SqlTypes.JSON) and @JdbcTypeCode(SqlTypes.SQLXML) will no longer be serialized to JSON/XML. Instead, they will be interpreted as plain JSON/XML to avoid the overhead of serialization/deserialization.

Reference

https://in.relation.to/2022/09/08/hibernate-orm-613-final/

like image 164
SANN3 Avatar answered Apr 30 '26 14:04

SANN3


What you are seeing here is that Hibernate serializes the string as JSON. I guess it's not very useful to do that and Hibernate could instead interpret a string a plain JSON, but in the end, I would rather suggest you model this as java.util.Map instead, which is then serialized to the proper JSON representation that you expect.

Either way, I created a Hibernate issue (HHH-15458) for this as I understand that this is the way people would expect this to work when they don't care about the representation of the JSON structure in their model.

like image 20
Christian Beikov Avatar answered Apr 30 '26 14:04

Christian Beikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!