简体   繁体   English

如何通过休眠envers审核一对一关系的两个表?

[英]How to Audit two tables with one-to-one relation through hibernate envers?

So I have two entity classes, Subscription and MailDetail . 所以我有两个实体类, SubscriptionMailDetail There is a one-to-one relation between them. 它们之间存在一对一的关系。 Here are the classes - 这是课程-

Subscription.class- Subscription.class-

@Data
@Entity
@AllArgsConstructor
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
public class Subscription {

@Id
@GeneratedValue(strategy =`enter code here` GenerationType.SEQUENCE, generator = "subscription_subscription_id_seq")
@SequenceGenerator(initialValue = 1, allocationSize enter code here= 1, name = "subscription_subscription_id_seq", sequenceName = "subscription_subscription_id_seq")
@Column
private Long subscriptionId;

@Column
private String template;

@Column
private String fileFormat;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "mail_id", referencedColumnName = "mail_id")
private MailDetail mailDetail;

}

MailDetail.class - MailDetail.class-

@Data
@Entity
@Audited
public class MailDetail {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mail_detail_mail_id_seq")
    @SequenceGenerator(initialValue = 1, allocationSize = 1, name = "mail_detail_mail_id_seq", sequenceName = "mail_detail_mail_id_seq")
    @Column
    private Long mailId;

    @Column
    private String emailId;
}

So this creates two audit tables, subscription_aud and mail_detail_aud . 因此,这将创建两个审计表, subscription_audmail_detail_aud Creation/Updates in mail_detail table is done through Subscription repository only. mail_detail表中的创建/更新仅通过订阅存储库完成。 So my requirement is that whenever there is a change in mail_details table's fields, I need an audit entry in subscription_aud. 因此,我的要求是,每当mail_details表的字段发生更改时,我都需要在subscription_aud中创建一个审核条目。 Or basically i want change in mail_detail table be considered as a change in subscription table. 或者基本上我希望将mail_detail表中的更改视为订阅表中的更改。

I am using hibernate envers for auditing. 我正在使用休眠envers进行审核。 How can i achieve this? 我怎样才能做到这一点?

Unfortunately, not with how Envers is currently designed. 不幸的是,目前的Envers并非如此。

Any to-many association that is either had its contents modified or elements in the collection changed will propagate an audit change for the owning side of the relationship by default; 默认情况下,任何修改了其内容或更改集合中元素的多对多关联都将传播对该关系拥有方的审核更改; however that can even be disabled by setting org.hibernate.envers.revision_on_collection_change to false . 但是,甚至可以通过将org.hibernate.envers.revision_on_collection_change设置为false来禁用它。 When this configuration setting is disabled, only element-collections will propagate a change to the owner because elements in such a collection are not entities, therefore can only be queried through the owning entity. 禁用此配置设置后,只有元素集合会将更改传播给所有者,因为此类集合中的元素不是实体,因此只能通过拥有实体来查询。

For to-one associations, this is a completely different story. 对于一对一的协会,这是一个完全不同的故事。

The only way to accomplish what you want would be to introduce some column on your Subscription instance that owns the MailDetail and change the value of that column when this use case happens. 完成所需操作的唯一方法是在拥有MailDetail Subscription实例上引入一些列,并在发生这种用例时更改该列的值。 Based on the fact you only modify MailDetail via the aggregate root Subscription , this should work however I agree that its less than ideal. 基于您只通过聚合根Subscription修改MailDetail的事实,这应该可以工作,但是我同意它不太理想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM