There are thing and property tables (M: M), as well as a table for storing their thing2property relationships.
I have these tables in the following classes
@Entity @Table public class Thing implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column private Integer id; @Column private String name; @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "thing2property", joinColumns = { @JoinColumn(name = "thing_id") }, inverseJoinColumns = { @JoinColumn(name = "property_id") }) private List<ThingProperty> thingProperties = new ArrayList<ThingProperty>(); } @Entity @Table public class ThingProperty implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column private Integer id; @Column private String name; @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "thing2property", joinColumns = { @JoinColumn(name = "property_id") }, inverseJoinColumns = { @JoinColumn(name = "thing_id") }) private List<Thing> things = new ArrayList<Thing>(); } I cut the classes for convenience, I admit that I could cut something important and that in this form they will not work, but the full versions of these classes I have data intact, so I hope you can abstract from the exact syntax.
For example, let the following contents appear in these tables:
property
id | name ---+------- 1 | color ---+------- 2 | width ---+------- 3 | weight thing
id | name ---+----- 1 | pen ---+----- 2 | ball ---+----- 3 | cap thing2property
thing_id | property_id ---------+------------- 1 | 1 ---------+------------- 1 | 3 ---------+------------- 2 | 3 ---------+------------- 3 | 3 But for each item-property pair, the input of an arbitrary string value is provided, which must be stored in the database. For example, the color of the handle is blue, the weight of the handle is 50g, the weight of the ball is 500g.
My first thought was to add the third field to the thing2property.value table. But how then to take out this value an automatic mapping? I tried to google, maybe I’m entering inquiries to the curves, but something is all wrong, maybe I should come to the solution of the problem from the other side?
I also thought of keeping the values for the related records in one of the columns in json, but how then the criteria to generate for the search by values is not clear.