java Hibernate Envers请求更改对象列表
我有一个承包商,他有一份他参与的项目清单。合同还包括其他列表,如员工、付款和其他字段(姓名、日期等)
我的目标是查看与承包商相关的项目是否发生了变化
范例
Contractor C is involved in the following projects:
1. Furman street <Active>
2. Park West <Active>
3. Central Train Station <Active>
有一天,用户将项目Park West从活动更改为已完成等
因此,现在每次我得到承包商的修订,我都会得到完整的信息(项目、联系人、字段等)。问题是,每次我触摸项目(列表)时,它都会进入数据库。我的问题是,既然我需要对数据库做一点小改动,我怎么能只请求项目修订?这样我就可以知道用户做了什么(例如:添加项目X并标记项目Y的完成)
到目前为止,我所做的是:
AuditReader reader = AuditReaderFactory.get(em);
AuditQuery query = reader.createQuery().forRevisionsOfEntity(Contractor.class, false, true).add(AuditEntity.id().eq(objID));
List<Contractor> contractors = query.getResultList();
我还试着只要求这样的项目(由于空指针异常而无法工作)
...add(AuditEntity.property("projects").hasChanged());
public class Contractor implements Serializable
{
//fields... name, dates...
@DiffIgnore
@OneToMany(mappedBy = "contractor")
@JsonIgnoreProperties(value = {"contractor"}, allowSetters=true)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<ContractorProject> projects = new HashSet<>(); //this is a many to many relationship for a reason
}
# 1 楼答案
根据您的评论,一旦建立了链接,
Project
就不能从Contractor
中取消分配,并且您只对与给定Contractor
实例相关的上述项目所做的更改感兴趣,获取相关信息的最佳方式可能是通过执行多个审计查询来构建该视图您需要的第一件事是与该
Contractor
关联的Project
ID的列表。您可以从审计表中获取这些信息,但我认为直接从正常实体数据中获取这些信息可能会更好上面的查询基本上是一个基于投影的查询,给定一个承包商标识符,您可以通过项目关联获得与承包商关联的所有项目标识符
如果您希望通过审计表获取这些信息,我们需要确定的第一件事是承包商的最大修订号,以便我们获取正确的数据快照
一旦获得了这些信息,就可以在循环中为每个
Project
执行审计查询,以确定所需的信息在Envers的下一个主要版本中,将有一些额外的查询方法,允许您获得一个包含以下内容的对象数组
这里的关键点是索引3,我们将为您提供已修改的属性,因此您不必自己计算这些属性