【发布时间】:2020-05-19 10:03:32
【问题描述】:
这是 2 个实体的代码,machine 和 device。
@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