tags:

views:

28

answers:

2

Hello,

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.

+1  A: 

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.

Mondain
A: 

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: http://stackoverflow.com/questions/3269654/combining-jaxb-and-jpa-in-one-model

Mike Baranczak