问题描述
这可能很简单,但无法找到方法。我试图找到每个实体的最大修订版,小于或等于给定修订版号。
AuditQuery query = getRequestOfEntity(
entityClass,false,false);
query.add(AuditEntity.revisionNumber().le(revisionNumber));
query.addOrder(AuditEntity.revisionNumber().desc());
query.getResultList();
以上代码按降序返回同一实体的多个修订版本。我想得到每个实体的最新版本,小于或等于给定的修订版本号。
作为一种解决方法,我按如下所示过滤resultSet。我希望这个过滤可以在AuditQuery本身完成。
AuditQuery query = getAuditReader()。createQuery()。forRevisionsOfEntity
entityClass,false,false);
query.add(AuditEntity.revisionNumber().le(revision));
query.addOrder(AuditEntity.revisionNumber().desc());
列表<?> list = query.getResultList();
StringBuilder builder = new StringBuilder();
EntityClass entity = null;
Set< Long> entitySet = new HashSet<>();
if(!list.isEmpty()){
for(int i = 0; i< list.size(); i ++){
Object [] result =(Object [] )list.get(i);
entity =(EntityClass)result [0];
AuditRevision auditRevision =(AuditRevision)结果[1];
RevisionType operationType =(RevisionType)result [2];
if(!entitySet.contains(entity.getId())){
//考虑最近修订的实体
entitySet.add(entity.getId());
builder.append(i + - >);
builder.append(id:+ entity.getId());
builder.append(,auditRevision:+ auditRevision);
builder.append(,operationType:+ operationType);
uilder.append(,count:+ entitySet.size());
解决方案:
我们需要使用[即
AuditEntity.revisionNumber()。maximize()。computeAggregationInInstanceContext()。
AuditQuery query = getAuditReader()。createQuery()。forRevisionsOfEntity(
entityClass,false,false);
query.add(AuditEntity.revisionNumber().le(revision));
query.add(AuditEntity.revisionNumber()。maximize()
.computeAggregationInInstanceContext());
query.addOrder(AuditEntity.revisionNumber().desc());
返回query.getResultList();
我想你正在寻找 AuditEntitry.revisionNumber.max()
约束。这应该最大化满足嵌套约束条件的版本号。
另见。
This might be simple, but unable to find a way.I am trying to find max revision of each entity less than or equal to given revision number.
AuditQuery query = getAuditReader().createQuery().forRevisionsOfEntity(
entityClass, false, false);
query.add(AuditEntity.revisionNumber().le(revisionNumber));
query.addOrder(AuditEntity.revisionNumber().desc());
query.getResultList();
Above code returns multiple revisions of same entity in descending order. I would like to get latest distinct revision of each entity less than or equal to given revision number.
As a workaround, I am filtering resultSet as shown below. I am hoping that this filtering can be done at AuditQuery itself.
AuditQuery query = getAuditReader().createQuery().forRevisionsOfEntity(
entityClass, false, false);
query.add(AuditEntity.revisionNumber().le(revision));
query.addOrder(AuditEntity.revisionNumber().desc());
List<?> list = query.getResultList();
StringBuilder builder = new StringBuilder();
EntityClass entity = null;
Set<Long> entitySet = new HashSet<>();
if (!list.isEmpty()) {
for (int i = 0; i < list.size(); i++) {
Object[] result = (Object[]) list.get(i);
entity = (EntityClass) result[0];
AuditRevision auditRevision = (AuditRevision) result[1];
RevisionType operationType = (RevisionType) result[2];
if (!entitySet.contains(entity.getId())) {
//consider most recent revision of entity
entitySet.add(entity.getId());
builder.append(i + "-->");
builder.append("id:" + entity.getId());
builder.append(", auditRevision:" + auditRevision);
builder.append(", operationType:" + operationType);
}
}
}
builder.append(", count:" + entitySet.size());
Solution:
We need to use fix of [https://hibernate.atlassian.net/browse/HHH-7827] i.e AuditEntity.revisionNumber().maximize().computeAggregationInInstanceContext().
AuditQuery query = getAuditReader().createQuery().forRevisionsOfEntity(
entityClass, false, false);
query.add(AuditEntity.revisionNumber().le(revision));
query.add(AuditEntity.revisionNumber().maximize()
.computeAggregationInInstanceContext());
query.addOrder(AuditEntity.revisionNumber().desc());
return query.getResultList();
I think what you are looking for the AuditEntitry.revisionNumber.max()
constraint. This should maximize the revision number satisfying the nested constraints.
See also the documentation on that matter.
这篇关于使用envers查找小于或等于给定修订的每个实体的最大修订版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!