0

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.

Anomitra
  • 1,111
  • 15
  • 31

1 Answers1

1

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

  • Yes, I'm aware of the separate table design. But I want this data in the same table, mostly because it's going to be a very unstructured set of keys and values and I don't necessarily want to force it into a separate table for the heck of it. – Anomitra Jun 30 '20 at 12:21
  • Ok then maybe try to implement a Converter like mentioned here: https://stackoverflow.com/questions/43628222/is-it-possible-to-add-functionality-to-the-column-annotation, This Converter could make a call to gson or smth like that – Martin Scheuchenpflug Jun 30 '20 at 13:19
  • Thanks Martin, this helped. Can you add a bit more on your answer about `Converter`s so that I can accept your answer? – Anomitra Jul 01 '20 at 09:57