2

This question was already discussed here, however, it did not solve my problem.

Let's have simple audited class Person with some String information like name, etc. and with List of String email addresses. Now I want to remove specific person from database along with all its revisions. I know, this is not common use-case for Envers, but it is crucial for my project. I understand, that I can use something like this:

"delete from full.package.name.Person_AUD u where u.originalId.id = :personid"

However, this does not delete records related to this person in table of emails, as there is no constraint about this relation. Or am I doing something wrong?

I was also thinking about deleting record from REVINFO table (there are constraints in audit tables about relation to REVINFO table), but this is not safe, as there can be more entities modified in one transaction and therefore more entities could have the same revision number.

My question is: Is there any easy way to delete all records from all audit tables for any person?

Community
  • 1
  • 1
ferou
  • 21
  • 1
  • 2

2 Answers2

1

This is fully working to me, and no native query is required

AuditQuery aq = auditReader.createQuery()
                   .forRevisionsOfEntity( ErpEmploye.class, true, false);       
 aq.add( AuditEntity.id().eq( employe.getCodeId() ) );
 aq.add( AuditEntity.relatedId("period").eq( erpPeriod.getCodeId() ) );
 List result =  aq.getResultList();//parameters must be added, this call is required
 if (result.size()>0){
    Query query = (Query) PrivateAccessor.invokePrivateMethod( aq, "buildQuery", new Object[0]);
    String queryString = (String) PrivateAccessor.getPrivateField( query, "queryString", true );
    PrivateAccessor.setPrivateField( query, "queryString", queryString.replace("select e__ from", "delete from"), true );
    getDAO().executeQuery(query);//transaction required             
}
Nassim MOUALEK
  • 4,702
  • 4
  • 25
  • 44
0

Since this is not a feature provide by hibernate and you are deleting the stuff on your own; then you need to delete the data from all the tables explicitly using similar queries.

 delete from full.package.name.Person_AUD u where u.originalId.id = :personid

 delete from full.package.name.Emails u where u.personId = :personid
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • Unfortunately example above is just simple example. Our real-world application is much more complex and suggested solution is not easy to implement. – ferou Aug 17 '12 at 08:17