2

How can one map a Java Map to a PostgreSQL json data type ?

Note that I want to map PostgreSQL-JSON-Data to a Java Map, and not to a String. Therefore I do not agree that my question is a duplicate of Mapping postgreSQL JSON column to Hibernate value type.

Entity example:

@Entity
public class Test implements Serializable {

    ...

    @Column(name = "DATA")
    private Map<String, Object> data = new HashMap<>();

    ...    
}

Usage:

Test t = new Test();
t.getData().put("name", "James");
t.getData().put("number", 1);
t.getData().put("now", new Date());

Expected json when persisted:

{
    "name" : "James"
    "number" : 1,
    "now" : 1438933526108
}
Community
  • 1
  • 1
yglodt
  • 13,807
  • 14
  • 91
  • 127
  • 1
    Why not just use Jackson to serialize/deserialize it to/from JSON? – Robby Cornelissen Aug 07 '15 at 07:51
  • Note that for Maps and other complex types you do need to use Jackson (or any other JSON serializer) to convert to and from String data in the `nullSafeGet` and `nullSafeSet` methods of your own `UserType`. – Kayaman Aug 07 '15 at 08:17

0 Answers0