Can hibernate envers return the latest revision of

2019-08-15 07:21发布

问题:

I am having a huge problem trying to get Envers to execute the query I need. If anyone can let me know if this is possible from within Envers or if I need to excute the SQL directly that would be a tremendous help!

Here is the problem. I have a made-up "Project" entity - any Entity class will do - that is audited. I am trying to get the latest revision of EVERY Project entity via an AuditReader query.

When I do this (the other parts of the code shouldn't matter):

AggregatedAuditExpression maxExpression = AuditEntity.revisionNumber().maximize();
maxExpression.add(AuditEntity.id().eq("12345"));
query.add(maxExpression);

and turn on the SQL output, I see this query being generated:

Hibernate: select project_a0_.id as id6_0_, project_a0_.REV as REV6_0_, auditrevis1_.id as id0_1_,
project_a0_.REVTYPE as REVTYPE6_0_, project_a0_.description as descript4_6_0_, 
auditrevis1_.timestamp     as timestamp0_1_, auditrevis1_.username as username0_1_ 
from MYSCHEMA.project_AUD project_a0_ cross join MYSCHEMA.REVINFO auditrevis1_ 
where project_a0_.REV=

(select max(project_a2_.REV) from MYSCHEMA.project_AUD project_a2_ 
               where project_a2_.id=?) 

and project_a0_.REV=auditrevis1_.id order by project_a0_.REV asc

Note the "select max" part. It is almost exactly what I need. Just the where clause is wrong I need it to say: where project_a2_.id=project_a0_.id

I edited it by hand, ran it and it works perfectly. Now it SEEMS like the "addIdsEqualToQuery" method in the IdMapper class lets me do what I want. So if I change out the AuditEntity.id().eq("12345") with this:

maxExpression.add(new IdentifierIdsEqAuditExpression());

where IdentifierIdsEqAuditExpression equals:

    class IdentifierIdsEqAuditExpression implements AuditCriterion {    
        @Override
        public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
            auditCfg.getEntCfg().get(entityName).getIdMapper()
                    .addIdsEqualToQuery(parameters, null, auditCfg.getAuditEntCfg().getOriginalIdPropName());
        }    
    }

it should be close - it is. I get:

Invalid path: 'originalId.id' [select e__, r from com.mycompany.Project_AUD e_, com.mycompany.audit.AuditRevisionEntity r where id = originalId.id and e_.originalId.REV.id = (select max(_e0.originalId.REV.id) from com.mycompany.Project_AUD e0 where id = originalId.id) and e_.originalId.REV.id = r.id order by e__.originalId.REV.id asc]

The problem is I can't get it to generate what I need and even if the prefix was changed from "originalId" to the correct one, why does envers start putting that all over the place, rather than in the single location?

回答1:

I'm afraid this is not currently possible with Envers. You would need to add something like parentIdEqual() to maxExpression, but there's no combinator like that.

Please open a feature request: https://hibernate.onjira.com/secure/Dashboard.jspa

As for the originalId, Envers entities have a composite id of the revision number and the original id (which is kept in the originalId sub-property).