Using JPA and JSON serialization
I would like to have a JPA Entity that has a GSon Object as a field/attribute. For instance:
@Entity
public class MyEntity {
private JsonObject myObject;
public Js开发者_如何学PythononObject getObject() {
return myObject;
}
public void setObject(JsonObject obj) {
myObject = obj;
}
}
I'm not familiar with how the persistence manager will persist these. Ideally it would be as a JSON string, but essentially what I want to do is hide how things are persisted from the public API. I could do this potentially with an inner class that has methods like String getObject()
that my outer class delegates to and does the JSON parse to return a JsonObject, but I'm wondering if there's another way.
It may be prudent to just store them as Strings (varchars) in the db. Just perform the GSon serialize / deserialize in your manager or dao.
If JsonObject implements Serializable, then it should behave correctly, but depending on how that class is written, it's possible that you'll wind up storing a lot of cruft you don't need. Converting it to a string seems like the preferable solution.
Possibly related: Combining JAXB and JPA in one model
精彩评论