Didn't Hibernate support this situation?
I have got a way to do that, it is: Use JSONB in JPA
But I cannot judge if it is safe.
And I do know I and write the same code by myself, but didn't the official provide some easy way to do that like just add an annotation like @Type(type="jsonb")
.
Hypersistence Utils project
The first thing you need to do is add the
hypersistence-utils
open-source project to your Maven or Gradle configuration file.Entity mapping
Afterward, use the
JsonType
like this:Persisting the entity
Now, when you persist the following
Book
entity:Hibernate will persist the JSON attribute properly:
Fetching the entity
Fetching the entity works as expected:
So, you don't have to create any custom Hibernate Type since you can find the right type in the
hypersistence-utils
project.