Anomitra
Anomitra

Reputation: 1161

Storing a list of POJOs as JSON objects in Postgres

I have a simple Java class which represents a real-life entity I have to store, such as this.

@Data
public class MappingValue {
    private String feature;
    private String value;
}

Now, I want to store several of these mappings as a list in a different entity. This entity is stored in a SQL table, and I want to store these mappings as a list of jsonb objects. I did something like this, but I can't seem to get it to work.

@Column(name = "specifications", columnDefinition = "jsonb")
private List<MappingValue> specifications;

Any ideas? Thanks in advance.

Upvotes: 0

Views: 2361

Answers (1)

Martin Scheuchenpflug
Martin Scheuchenpflug

Reputation: 159

You use an JPA Attribute Converter for that. You implement such a Converter to Store a Complex type in one Column of the Db

Example: https://www.baeldung.com/jpa-attribute-converters

Upvotes: 2

Related Questions