【问题标题】:Hibernate Envers doesn't save referenced column value in case of @OneToMany Relationship在 @OneToMany 关系的情况下,Hibernate Envers 不会保存引用的列值
【发布时间】:2020-05-19 10:03:32
【问题描述】:

这是 2 个实体的代码,machinedevice

@Audited
@Entity(name = "device")
@Table(name = "device")
public class Device {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "machineCode", referencedColumnName = "machineCode")
    private Machine machine;

}

@Audited
@Entity(name = "machine")
@Table(name = "machine")
public class Machine {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String machineCode;

}

现在,当我保存 device 实体时。 machineCode 列(在device 的审计表中)从机器的域对象中获取id 的值,而不是对应的machineCode。 我不确定代码是否有任何问题。提前致谢。

PS - 还有其他混合实体附加到machine 实体。因此,不需要添加@OneToMany,或者我应该说,这将是一个开销。


编辑 1:这个问题是关于 envers 审计框架的,如果我说得不够清楚的话。

【问题讨论】:

  • 您在“Machine”实体中的“machineCode”字段必须是唯一的。您可以尝试使用@NaturalId 对其进行注释。
  • 对不起@lutfucan,它没用。
  • 查看类似问题的答案和 cmets:stackoverflow.com/questions/31160321/…

标签: hibernate hibernate-envers


【解决方案1】:

您需要确保被引用的实体(机器)实现了可序列化。

查看以下链接以获得进一步说明:-

https://thorben-janssen.com/hibernate-tips-model-association-dont-reference-primary-key-columns/

【讨论】:

  • 链接哥们没有提到envers框架
猜你喜欢
  • 2019-12-23
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 2016-01-23
  • 1970-01-01
相关资源
最近更新 更多