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.

    1 answer 1

    I tried to google

    Apparently, the keywords were not successfully picked up, Google personally offered to me to add it right away and in the very first line of the issue I presented a link to the solution option .

    The essence of the proposed solution lies in the fact that it is necessary to replace one Many To Many relation with cascade relations One To Many and Many To One . For this, thing2property should be allocated to an independent entity by adding the value property to the table (as you suggested). Mappings will be approximately like this:

     @Entity @Table public class Thing implements Serializable { @OneToMany(mappedBy = "thing") private Set<Thing2Property> props; @Entity @Table public class Thing2Property implements Serializable { @ManyToOne @JoinColumn(name = "thing_id") private Thing thing; @Column private String value; @ManyToOne @JoinColumn(name = "property_id") private ThingProperty property;