ferou
ferou

Reputation: 21

Hibernate Envers : How to delete entries from my audit tables?

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?

Upvotes: 2

Views: 3259

Answers (2)

Nassim MOUALEK
Nassim MOUALEK

Reputation: 4864

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             
}

Upvotes: 1

Bharat Sinha
Bharat Sinha

Reputation: 14363

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

Upvotes: 0

Related Questions