If you want to destroy the revision by identifier, you can access the envers table directly using your own query. There are two tables containing links to the revision. Assuming your audit table uses the usual _AUD suffix, you can programmatically determine the name of the entity table.
Here are some snippets written in Kotlin:
fun getAuditTableName(em: EntityManager, aClass: Class<*>): String { return getAuditTableName(em, aClass.name) + "_AUD" } fun getEntityTableName(em: EntityManager, aClass: Class<*>): String { val session = em.unwrap(Session::class.java) as Session val sessionFactory = session.sessionFactory val hibernateMetadata = sessionFactory.getClassMetadata(className) val persister = hibernateMetadata as AbstractEntityPersister return persister.tableName }
Now that we have the table name, we can delete the rows in the tables. (Put this in the JPA transaction block, replace the content as needed, and configure SQL for your provider). Therefore, given MyEntityClass and myRevisionId, we can do something like this:
val em:EntityManager = getEntityManager() val auditTableName = getAuditTableName(MyEntityClass::class.java) em.createNativeQuery("delete from `$auditTableName` where REV=${myRevisionId}").executeUpdate() em.createNativeQuery("delete from REVINFO where REV=${myRevisionId}").executeUpdate()
If you want to remove a parameter other than the revisionID, just query the revisionIds parameters in the entity_AUD table, and then delete the found rows in the specified way.
Keep in mind that the versionId file can be associated with more than 1 entity, and all entries will be deleted in the previous method. To remove a revision for a single object, you will need the names and identifiers of the name of the entity and entity.
Here is the code for dynamically getting the field name:
fun getEntityKeyNames(em: EntityManager, entityClass: Class<*>): List<String> { val session = em.unwrap(Session::class.java) as Session val sessionFactory = session.sessionFactory val hibernateMetadata = sessionFactory.getClassMetadata(entityClass.name) val persister = hibernateMetadata as AbstractEntityPersister return persister.keyColumnNames.toList() }