I basically have the following mapping
@Entity
public class Scheme {
@Id
private Long id;
private String name;
}
@Embeddable
class MetricId {
private Long composition;
@Column(name = "ID_SCHEME")
private Long scheme;
}
@Entity
public class Metric {
@EmbeddedId
private MetricId id;
private BigDecimal metric1;
private BigDecimal metric2;
}
@Entity
public class MetricComposition {
@Id
private Long id;
@ElementCollection
@MapKeyJoinColumn(name = "ID_SCHEME", insertable = false, updatable = false)
@OneToMany(mappedBy = "id.composition")
private Map<Scheme, Metric> metrics = new HashMap<>();
}
This mapping is working, but have one issue, when I try to save a MetricComposition
, hibernate performs an extra update in a newly inserted Metric
An example would be better:
- After it does an
insert into METRIC (metric1, metric2, ID_SCHEME, composition) values ('1.0', '2.0', 1, 1)
- It performs an
update METRIC set ID_SCHEME=1 where ID_SCHEME=1
Is there a way to avoid this?