简体   繁体   English

如何审计具有 OneToMany 单向关系的 class?

[英]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 .ArticleFile之间存在单向关系 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.

相关问题 如何在 JPA 中定义单向 OneToMany 关系 - How to define unidirectional OneToMany relationship in JPA 如何实现一对多单向关系(Spring boot + JPA) - How to OneToMany unidirectional relationship (Spring boot + JPA) 映射一对多单向关系 - Mapping a OneToMany unidirectional relationship 在JoinTable中使用Criteria在休眠的单向OneToMany关系中获取子类 - Using Criteria get child class in unidirectional OneToMany relationship in hibernate with JoinTable 如何在没有任何级联的情况下定义单向OneToMany JPA关系? - How to define a unidirectional OneToMany JPA relationship without any cascade? 如何使用 JPA 和 Hibernate 为 2 个表建立单向 @oneToMany 关系 - How to make unidirectional @oneToMany relationship for 2 tables using JPA and Hibernate Hibernate Envers-如何审核具有@OneToMany关系的实体中的更改? - Hibernate Envers - How to audit changes in an entity that is in a @OneToMany relationship? Hibernate - 用于从Unidirectional OneToMany关系中获取集合的HQL - Hibernate - HQL to fetch a collection from Unidirectional OneToMany relationship Hibernate 单向一对多关系与外键中的非空约束 - Hibernate Unidirectional OneToMany Relationship with Not-null Constraint in Foreign Key Hibernate 4.2,JPA 2.0具有注释的一对多关系 - Hibernate 4.2, JPA 2.0 Relationship OnetoMany Unidirectional with Annotations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM