Representing differences between database representation and object
representation in JPA
pI am working with a legacy database that makes frequent use of blobs of
JSON stored as text. I am trying to write some JPA classes that will hide
this implementation detail, make it easier to work with the database, and
allow us to refactor the database schema in the future. My current
approach is:/p precodeclass MyTableObject{ @Lob @Column(name = stuff)
private String jsonString; public Listlt;Stuffgt; getStuff(){ return
jsonToStuff(jsonString); } public setStuff(Listlt;Stuffgt; stuff){
jsonString = stuffToJsonString(stuff); } } /code/pre pHere, the
representation is always as a JSON string in both the the database and the
Object and although it works (so far) , it is pretty inefficient as the
JSON has to be parsed every time the state of the object is modified. I
appreciate that I could improve performance of my current solution by
cacheing the Stuff objects in memory, but I would still have to serialize
them to JSON every time the setter is called and ensure that the 2
representations of the state (Objects and JSON) were always in sync. What
I would like to be able to do is to tell the framework to convert my field
on the way in and out, something like:/p precodeclass MyTableObject{
private Listlt;Stuffgt; stuff; @Lob @Column(name = stuff) public String
getStuffAsJsonString(){ return stuffToJsonString(stuff); } @Column(name =
stuff) public setStuffFromJsonString(String jsonString){ stuff =
stuffFromJsonString(jsonString); } } /code/pre pHowever, as far as I know
annotations are only valid on the getter. Can I achieve something like the
above - with one representation in the Object and a different
representation in the database? I am new to JPA so I could easily be
missing something obvious./p pMany thanks in advance/p