简体   繁体   English

Hibernate Envers-如何审核具有@OneToMany关系的实体中的更改?

[英]Hibernate Envers - How to audit changes in an entity that is in a @OneToMany relationship?

Lets say I have two entities, A and B. A has a @OneToMany relationship to B and I have audited all the properties in B. When I use Envers to obtain the revisions all I get are the changes in the relation between A and B but I also need to obtain the changes in B's properties. 可以说我有两个实体,A和B。A与B有一个@OneToMany关系,并且我已经审计了B中的所有属性。当我使用Envers获取修订时,我得到的只是A和B之间的关系更改。但是我还需要获取B属性的更改。 Is there a way todo this? 有没有办法做到这一点?

Thanks in advance! 提前致谢!

I am working off the presumption of the following mappings: 我正在研究以下映射的假定:

@Entity
@Audited
public class EntityA {
    /* other properties & getters/setters */
    @OneToMany(mappedBy = "a", cascade = CascadeType.ALL)
    private List<EntityB> bList;
}

@Entity
@Audited
public class EntityB {
    /* other properties & getters/setters */
    @ManyToOne
    private EntityA a;
}

Anytime you add/remove an EntityB from EntityA , this will trigger a revision change associated with EntityA as well as the associated EntityB that is added/removed due to the relationship. EntityBEntityA添加/删除EntityA ,这都会触发与EntityA以及由于该关系而添加/删除的关联EntityB关联的修订更改。

But if you decide you want to modify some attribute of EntityB that has no direct impact upon EntityA , there will only be a revision generated for EntityB and not EntityA , see below. 但是,如果您决定要修改EntityB某些对EntityB没有直接影响的EntityA ,则只会为EntityB而不是EntityA生成一个修订,请参见下文。

entityManager.getTransaction().begin();
final EntityA a = entityManager.find( EntityA.class, aId );
for ( EntityB b : a.getbList() ) {
    b.setName( b.getName() + "-modified" );
}
entityManager.merge( a );
entityManager.getTransaction().commit();

If you need the individual revisions of EntityB in this case, you'll need to actually specifically query for EntityB through the AuditReader interface instead. 如果在这种情况下需要EntityB的各个修订版, EntityB实际上需要通过AuditReader接口专门查询EntityB

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

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