首页 文章

java hibernate envers为审计表设置其他主键

提问于
浏览
0

我想在JPA Hibernate中映射一个Map . 设置看起来像

@Entity(name = "reservation")
@Table(name = "reservation")
@Access(AccessType.FIELD)
@Audited
public class ReservationEntity {

  // other fields

  @ElementCollection(fetch = FetchType.EAGER)
  @MapKeyColumn(name = "discountType")
  @Column(name = "discountAmount")
  @CollectionTable(
          name="discountTypeAndAmount",
          joinColumns=@JoinColumn(name="reservation_id")
  )
  private Map<DiscountType, BigDecimal> discountTypeAndAmount;

}

我可以在第一次将实体写入数据库,但是当我更新实体时,我在 entitymanager.getTransaction().commit() 时收到以下错误:

Caused by: javax.persistence.EntityExistsException: A different object with
the same identifier value was already associated with the session : 
[discountTypeAndAmount_AUD#{REV=DefaultRevisionEntity(id = 3, 
revisionDate = Dec 20, 2016 8:52:45 PM), element=10.00,  
ReservationEntity_reservation_id=1, mapkey=CASE_STUDY}]

在例外中, CASE_STUDY 是其中一个枚举 . discountTypeAndAmount_AUD 是自动生成的审计日志表 .

看起来审计表 discountTypeAndAmount 是使用由 REV (修订版ID), reservation_iddiscountTypediscountAmount 组成的复合键生成的,并且抛出错误,因为envers不知道如何处理 BigDecimal 作为主键的一部分 .

是否有注释将审计表的主键设置为仅 REV (修订版ID), reservation_iddiscountType 的组合?由于该字段无论如何都是 Map ,因此实际上不需要将 discountAmount 作为主键的一部分 .

1 回答

  • 0

    如果key是基本类型,@ MapKeyColumn用于指定键列的映射,但是我不确定,但我想DiscountType是实体本身,所以你需要使用@MapKeyJoinColumn映射你的键 .

相关问题