(It seems to me that this question should have been already asked somewhere, but I just can't find it, so excuse me in advance if it's a duplicate)
I am using Hibernate and Gson. Say, I have an object like this
@Entity
class Foo {
...
com.google.gson.JsonElement bar;
...
}
Foo.bar
can be of arbitrary format. That's why I need to persist it in a single database field, i.e. I need to serialize it, presumably to JSON, and persist that string into the bar
column of my foo
table. Currently, unsurprisingly, I just get an error from Hibernate because the JsonElement
type doesn't map to the string-type database column. How can I achieve this mapping (using Hibernate or, more generally, JPA)?
@AttributeConverter
allowing you to convert any type to some persistable type. – Neil Stockton