我基本上有以下映射
@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<>();
}
此映射正常,但有一个问题,当我尝试保存MetricComposition
时,hibernate会在新插入的Metric
中执行额外更新
一个例子会更好:
insert into METRIC (metric1, metric2, ID_SCHEME, composition) values ('1.0', '2.0', 1, 1)
update METRIC set ID_SCHEME=1 where ID_SCHEME=1
有没有办法避免这种情况?