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.
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.Mapinstead, 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.