使用@MapKeyJoinColumn映射集合,哪个键参与值的复合键

时间:2015-11-09 19:04:10

标签: hibernate dictionary composite-key mapkeyjoincolumn

我基本上有以下映射

@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

有没有办法避免这种情况?

0 个答案:

没有答案