You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate Envers处理ElementCollection时更新审计日志异常问题

问题:Hibernate Envers对@ElementCollection的更新审计记录异常

代码实现

Resource类

@Audited
@Table(name = "resource_item")
public class Resource {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Access(AccessType.PROPERTY)
  private Long id;

  private String nom;

  @ElementCollection(targetClass = ItemSelection.class)
  @CollectionTable(name = "item_selection", joinColumns = @JoinColumn(name = "resource_id"))
  @OrderColumn(name = "list_order")
  private final List<ItemSelection> itemSelections = new ArrayList<>();
  ...
  ...

ItemSelection类

@Embeddable
@Access(AccessType.FIELD)
public class ItemSelection {

  //@Column(name = "hierarchy_code", nullable = false)
  private String hierarchyCode;
  //@Column(name = "hierarchy_level", nullable = false)
  private int hierarchyLevel;
  //@Column(name = "hierarchy_value_code", nullable = false)
  private String hierarchyValueCode;

  ...
  ...

正常审计场景(新增Resource)

当添加包含3个itemSelections元素的Resource时,Envers审计日志正常:

localhost sa@envers=# select * from item_selection_aud ;
 rev | revtype | resource_id | list_order | hierarchy_value_code | hierarchy_code | hierarchy_level 
-----+---------+-------------+------------+----------------------+----------------+-----------------
   2 |       0 |           1 |          1 | hvalueCode3          | hcode3         |               3
   2 |       0 |           1 |          0 | hvalueCode1          | hcode1         |               1
   2 |       0 |           1 |          2 | hvalueCode2          | hcode2         |               2
(3 rows)

更新操作后的异常审计日志

但更新item_selection中的一行时,SQL仅执行一条更新语句,Envers审计日志却生成两条记录(删除+新增)而非更新记录:

localhost sa@envers=# select * from item_selection_aud ;
 rev | revtype | resource_id | list_order | hierarchy_value_code | hierarchy_code | hierarchy_level 
-----+---------+-------------+------------+----------------------+----------------+-----------------
   2 |       0 |           1 |          1 | hvalueCode3          | hcode3         |               3
   2 |       0 |           1 |          0 | hvalueCode1          | hcode1         |               1
   2 |       0 |           1 |          2 | hvalueCode2          | hcode2         |               2
   3 |       0 |           1 |          0 | hvalueCode1          | CODE           |               1
   3 |       2 |           1 |          0 | hvalueCode1          | hcode1         |               1
(5 rows)

可见修改的行被记录为revtype=2(删除)和revtype=0(新增),而非更新对应的revtype=1。

已尝试的解决方法

  • 为ItemSelection的字段添加@Column(nullable = false)注解,无效果
  • 将审计策略从DefaultAuditStrategy切换为ValidityAuditStrategy,仅新增revend列,问题依旧
  • 考虑将@ElementCollection替换为@OneToMany,但项目中大量使用@ElementCollection,重构成本极高

疑问

是否遗漏了某些配置项?使用的Hibernate-envers版本为5.6.12-Final。

内容的提问来源于stack exchange,提问作者Mançaux Pierre-Alexandre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 01:13:59