I am new to JPA/Hibernate and I have a situation similar to what is described in this post:
Is it possible to dynamically define column names in Hibernate / JPA?
Just that my table contains 50 key/value pairs which I want to declare as a single object and then compact all the columns into a collection (a map would work as well). The answer of the above question describes how to declare a custom naming strategy for the columns but I am having difficulties putting the whole thing together. I want to avoid having a model with 100 (50) fields and cannot imagine how exactly the collection of pairing classes (or map) will be declared to map the 50 individual pairs.
What I am trying to achieve is similar to this:
Using List or Set in JPA 2.1 for multiple columns in same table
Unfortunately the answer described does not work for me since I cannot afford to externalise the collection in another table.
My table looks like this:
id serial primary key,
name VARCHAR(255) NOT NULL,
key_1 BIGINT,
value_1 VARCHAR(255),
key_2 BIGINT,
value_2 VARCHAR(255),
. .
key_50 BIGINT,
value_50 VARCHAR(255)
I would like to compact it into something like:
@Entity
@Table(name="table")
public class MyModel {
@Id
Long id = null;
@Column(name="name")
String name;
Map<K,V> values; //Or a List<MyKeyValuePair> values;
}
Any suggestions?
Thanks