I had a huge problem trying to get Envers to fulfill my request. If anyone can tell me if this is possible from within Envers or if I need to fix SQL for free, that would be a huge help!
Here is the problem. I have created a "Project" object - any Entity class will do - this is checked. I am trying to get the latest revision of the "EVERY project" object using an AuditReader request.
When I do this (other parts of the code should not matter):
AggregatedAuditExpression maxExpression = AuditEntity.revisionNumber().maximize(); maxExpression.add(AuditEntity.id().eq("12345")); query.add(maxExpression);
and turn on the SQL output, I see that this query is 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
Pay attention to the "select max" part. This is almost what I need. Just where the sentence is wrong, I need him to say: where project_a2_.id = project_a0_.id
I edited it manually, ran it, and it works great. Now he SEEMS, as the addIdsEqualToQuery method in the IdMapper class, allows me to do what I want. Therefore, if I changed the value of AuditEntity.id (). Eq ("12345") as follows:
maxExpression.add(new IdentifierIdsEqAuditExpression());
where IdentifierIdsEqAuditExpression is equal to:
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()); } }
he must 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 that I canβt get her to generate what I need, and even if the prefix was changed from "originalId" to the correct one, why the envers begin to place this all over the place, and not in one place