[英]How to audit class that has OneToMany unidirectional relationship?
I'm using Spring Data JPA for Auditing.我正在使用 Spring 数据 JPA 进行审计。 There's a unidirectional relationship between classes Article and File .
类Article和File之间存在单向关系。 The Article class looks like this:
文章class 如下所示:
@Getter
@Entity
@SuperBuilder(toBuilder = true)
@Table(name = "article")
public class Article extends AuditEntity {
...
@Builder.Default
@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true)
@JoinTable(name = "article_additional_file",
joinColumns = @JoinColumn(name = "article_id"),
inverseJoinColumns = @JoinColumn(name = "additional_file_id"))
private List<File> additionalFiles = new ArrayList<>();
...
}
The problem is, when changes occur in the file list (owned files get deleted or added), the modifiedDate field (which is in AuditEntity class and it's annotated with @LastModifiedDate annotation) is not updated (it works with all other fields).问题是,当文件列表中发生更改(拥有的文件被删除或添加)时, modifiedDate字段(位于 AuditEntity class 中,并使用@LastModifiedDate 注释进行注释)不会更新(它适用于所有其他字段)。 And I cannot make it a bidirectional relationship since other classes own the File class as well.
而且我不能使它成为双向关系,因为其他类也拥有文件 class。 So my question is, how to trigger the update of field modifiedDate when changes occur in the file list?
所以我的问题是,当文件列表发生变化时,如何触发字段 modifiedDate 的更新?
EDIT I'd prefer not to use Enver, if that's possible.编辑如果可能的话,我宁愿不使用 Enver。 I need to use as little additional libraries as possible
我需要尽可能少地使用额外的库
Instead of using @JoinTable use @AuditJoinTable而不是使用 @JoinTable 使用 @AuditJoinTable
Info from the hibernate documentation: hibernate 文档中的信息:
When a collection is mapped using these two annotations (@OneToMany + @JoinColumn), Hibernate doesn't generate a join table.当使用这两个注释 (@OneToMany + @JoinColumn) 映射集合时,Hibernate 不会生成连接表。 Envers, however, has to do this, so that when you read the revisions in which the related entity has changed, you don't get false results.
然而,Envers 必须这样做,这样当您阅读相关实体已更改的修订时,您不会得到错误的结果。
To be able to name the additional join table, there is a special annotation: @AuditJoinTable, which has similar semantics to JPA's @JoinTable.为了能够命名附加的连接表,有一个特殊的注解:@AuditJoinTable,它与 JPA 的 @JoinTable 具有相似的语义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.