views:

204

answers:

2

Hi everyone,

I am currently working with Hibernate Envers. My problem is the following :

How to delete entries in the audit table related to the entity I want to delete?

My entity has no relation with other entities.

I figured out that I have to do that in onPostDelete method of my custom listener :

import org.hibernate.envers.event.AuditEventListener;
import org.hibernate.event.PostCollectionRecreateEvent;
import org.hibernate.event.PostDeleteEvent;
import org.hibernate.event.PostInsertEvent;
import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PreCollectionRemoveEvent;
import org.hibernate.event.PreCollectionUpdateEvent;

public class MyListener extends AuditEventListener {

  ...
  @Override
  public void onPostDelete(PostDeleteEvent arg0) {
    // TODO Auto-generated method stub
    super.onPostDelete(arg0);
  }
  ...

}

I've read the documentation, forums, many things but I can't figure it out. May be it's not possible, I don't know.

Has someone ever done this before?

Thank you :)

A: 

Audit entries are typically only added, not deleted, even when the related entity is deleted so I don't think that the Envers API provide support for that.

Now, if really you want to remove entries for deleted entities (this kinda defeats the purpose of auditing), you can maybe delay this a bit and instead of removing entries at deletion time, run a daily native query, for example every night.

Pascal Thivent
Thanks Pascal for your time.As you said, looks like Envers API doesn't provide support for that.After looking on several forums, I've found a clue.I'll have to write a HQL query :"delete from full.package.name.User_AUD u where u.id = :userid"It's half working because I'm now stuck with a Hibernate problem...
Laurent T
A: 

Ok I'm 50% done with this for those who want to know.

Thanks to the creator of Hibernate Envers, Adam Warski, I quote :

"id" is a hibernate keyword for the id of an entity, whatever the names is; in case of audit entities, the id is composite and is called "originalId". Try:

"delete from full.package.name.User_AUD u where u.originalId.id = :userid" 

But now, I also would like to delete entries related to audit table in my revinfo table.

If someone has a clue, let me know

Thanks.

Laurent T