gpt4 book ai didi

java - Hibernate Envers - 请求更改对象列表

转载 作者:行者123 更新时间:2023-12-02 11:08:51 24 4
gpt4 key购买 nike

我有一个承包商,其中包含他所参与的项目列表。该契约(Contract)还有其他列表,例如员工、付款和其他字段(姓名、日期等)。

我的目标是查看承包商关联的哪些项目发生了变化。

示例

Contractor C is involved in the following projects: 
1. Furman street <Active>
2. Park West <Active>
3. Central Train Station <Active>

有一天,用户将 Park West 项目从 Activity 更改为已完成等。

现在,每次我收到承包商的修订版时,我都会获得完整的信息(项目、联系人、字段等)。问题是,每次我触摸项目(列表)时,它都会转到数据库。我的问题是,由于我需要对数据库进行最少的接触,我如何才能仅请求项目修订?这样我就可以知道用户做了什么(例如:添加项目 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

}

最佳答案

根据您的评论,一旦建立链接,项目就无法从承包商取消分配,并且您只能如果对与给定 Contractor 实例相关的上述项目的更改感兴趣,获取相关信息的最佳方法可能是通过执行多个审核查询来构建该 View 。

您首先需要的是与该Contractor 关联的Project ID 列表。您可以从审计表中获取这些信息,但我相信直接从普通实体数据中获取这些信息可能会更好。

SELECT p.id FROM Contractor c JOIN c.projects p WHERE c.id = :contractorId

上面的查询基本上是一个基于投影的查询,给定承包商标识符,您可以通过项目关联获取与承包商关联的所有项目标识符。

如果您希望通过审核表获取此信息,我们需要确定的第一件事是承包商的最大修订号,以便我们获取正确的数据快照。

// you might want to check if this collection is empty
// it should not be assuming you aren't removing data from the audit tables
// but sometimes people archive data, so its best to be thorough
List<Number> revs = reader.getRevisions( Contractor.class, contractorId );

// The list of revisions are always in ascending order, so grab the last entry.
Number maxRevision = revs.get( revs.size() - 1 );

// Build the projection query
// This returns us the list of project ids, just like the HQL above
List projectIds = reader.createQuery()
.forEntitiesAtRevision( Contractor.class, maxRevision )
.traverseRelation( "projects", JoinType.INNER )
.addProjection( AuditEntity.property( "id" ).distinct() )
.up()
.add( AuditEntity.id().eq( contractorId ) )
.getResultList();

获得此信息后,就可以在每个项目的循环中执行审核查询,以确定您需要的信息。

for ( Object projectId : projectIds ) {
List results = reader.createQuery()
.forRevisionsOfEntity( Project.class, false, false )
.add( AuditEntity.id().eq( projectId ) )
.addOrder( AuditEntity.revisionNumber().asc() );
// At this point you have an list of object array values
// Index 0 - This is the instance of Project
// Index 1 - This is the revision entity, you can get the rev # and timestamp
// Index 2 - Type of revision, see RevisionType
//
// So you can basically iterate the list in ascending order keeping track of
// the prior Project and build a changeset for each project.
//
// This approach is often more efficient if you're needing to compare multiple
// attributes on an entity rather than issuing a query to the database for
// each change made to a single property.
}

在 Envers 的下一个主要版本中,将会有一些额外的查询方法,这些方法将允许您获取由以下内容组成的对象数组

  // Index 0 - This is the instance of Project 
// Index 1 - This is the revision entity, you can get the rev # and timestamp
// Index 2 - Type of revision, see RevisionType
// Index 3 - Array of properties changed at this revision

这里的关键点是索引3,我们将在其中向您提供已修改的属性,因此您不必自己计算这些属性。

关于java - Hibernate Envers - 请求更改对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50730720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com