3

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 JsonObject 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.

Anon
  • 1,290
  • 2
  • 16
  • 24

2 Answers2

2

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.

Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131
1

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

Community
  • 1
  • 1
Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71