1

I am searching on this problem for two days now, without a proper solution.

I have a PostgreSQL-Database with a table:

CREATE TABLE mytable (key text PRIMARY KEY, content text)

Further, I have a Java SpringBoot-Application with a JPA entity class:

@Entity
@Table(name = "mytable")
public class MyEntity {

  @Id
  private String key;

  // tell JPA to convert MyPojo to JSON using the default Jackson ObjectMapper
  private MyPojo content;

  // constructors, getters, setters

}

The MyPojo class contains sub-structure involving basic types, Maps, Lists, and other nested POJO classes. It does not seem convenient to write a Converter for the MyPojo class and all the other nested POJO classes. Instead, I want to use Jackson to serialize/deserialize it.

How do I configure Jackson/JPA to store a MyPojo object as a JSON string in the text field of the database table? I was hoping for a suitable Annotation like @Convert(converter=Jackson.ObjectMapper). Introducing an additional String field to the MyEntity class was another approach that would not work.

I sucessfully used Jackson to send/parse MyPojo objects as JSON strings to a REST interface, but I'm struggling to put the same string into the text field of a database.

2 Answers 2

3

You could define a converter to convert between the Pojo and JSON and use Jackson to make the conversion:

@Converter
public class MyPojoToJsonConverter implements AttributeConverter<MyPojo, String>{

    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String convertToDatabaseColumn(MyPojo myPojo) {
        String json = "";
        try {
            json = objectMapper.writeValueAsString(myPojo);
        } catch (JsonProcessingException jpe) {
            // Handle exception
        }
        return json;
    }

    @Override
    public MyPojo convertToEntityAttribute(String myPojoAsJson) {
        MyPojo myPojo = null;
        try {
            myPojo = objectMapper.readValue(myPojoAsJson, MyPojo.class);
        } catch (JsonParseException e) {
            // HandleException
        } catch (JsonMappingException e) {
            // HandleException
        } catch (IOException e) {
            // HandleException
        }
        return myPojo;
    }
}

Then you can setup this converter in the attribute of your Entity using the @Convert annotation:

@Entity
@Table(name = "mytable")
public class MyEntity {

    @Id
    private String key;

    @Convert(converter=MyPojoToJsonConverter.class)
    private MyPojo content;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. This approach works quite well. I can write a single Converter class, and all substructures in the POJO are (de)serialized automatically.
0

MyPojo should the a String not an Object in your Entity. Put your JSON in the String, that should be all.

1 Comment

I could do that. But how do I obtain a MyPojo object from this on the fly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.