I’m trying to use hibernate to create a map with the properties of a tripartite.
SQL:
-- Rec
insert into rec(id, name) values(1, 'Rec 1');
insert into rec(id, name) values(2, 'Rec 2');
insert into rec(id, name) values(3, 'Rec 3');
-- Param
insert into params(id, name) values(1, 'PARAM1');
insert into params(id, name) values(2, 'PARAM2');
-- Rec Param
insert into rec_param(id, rec, param, param_value) values(1, 1, 1, 'REC 1 - Param1');
insert into rec_param(id, rec, param, param_value) values(2, 1, 2, 'REC 1 - Param2');
insert into rec_param(id, rec, param, param_value) values(3, 2, 2, 'REC 2 - Param2');
I got my three entities
Params
@Getter
@Entity
@Table(
name = "params",
uniqueConstraints = {@UniqueConstraint(columnNames={"name"}) }
)
public class Params {
@Id
@GeneratedValue
long id;
@NaturalId
@Column(name = "name", insertable = false, updatable = false)
String name;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Params params = (Params) o;
return name.equals(params.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
REC
@Getter
@Entity
@Table(name = "rec")
public class Rec {
@Id
@GeneratedValue
long id;
@Basic
String name;
@OneToMany(mappedBy = "rec", fetch = FetchType.EAGER)
@MapKeyColumn(name="param")
Map<String, RecParam> paramMap = new HashMap<>();
}
RecParam:
@Getter
@Entity
@Table(
name = "rec_param",
uniqueConstraints = {@UniqueConstraint(columnNames={"rec", "param"}) })
public class RecParam {
@Id
@GeneratedValue
long id;
@NaturalId
@JoinColumn(name = "rec")
@ManyToOne
Rec rec;
@NaturalId
@JoinColumn(name = "param", insertable = false, updatable = false)
@ManyToOne
Params param;
@Basic
String paramValue;
}
With this code, I manage to create a Map with
-> Key -> String representing the ID of the Params (“1”, or “2”)
-> Value -> The associated RecParam Object
What I’m trying to get is a map composed by the param name, and the rec param value.
For example, for rec1, it would be:
- ‘PARAM1’ -> ‘REC 1 – Param1’
- ‘PARAM2’ -> ‘REC 1 – Param2’
Do you know if it is possible?
if not, can have at lease the param name as a key, instead of the object id?
Thanks,
Nicolas