I'm using hibernate-envers for audit purposes in an application. I'm also using hibernate-search in order to search/read the information of JPA entities in the application.
I was wondering if there's any kind of configuration/integration that can make hibernate-envers work with the audit enties/tables, over indexes too, in order to read with hibernate -search that information from the indexes.
I would like to avoid doing it "manually", for example, using envers event listeners in order to create/manipulate a new index manually for the audited entity, using a new JPA Entity modelling the Audit entity information including #Indexed annotation, fields etc.).
Ideally was wondering if there's support for envers/search integration out of the box, without custom development, to achieve storing all audit information in new _aud indexes.
Thanks in advance, any piece of advice is appreciated.
It's certainly not possible out of the box.
If it ever becomes possible, you won't benefit from all the Envers features such as "get me this entity at this revision". You will simply index all the revisions of each entity, and you will only be able to query (and retrieve) these revisions. That would be queries such as "get all revisions of the entity with id 1 where name contained "some text".
Also, this will not remove the need for audit tables. The indexes will exist in addition to the audit tables.
That being said, I just gave it a try and we could make it possible in Hibernate Search 6 with just a few changes. If you're still interested, you can have a look there: https://hibernate.atlassian.net/browse/HSEARCH-4238
Related
I'm using Spring Data JPA to expose REST APIs. In my application, there are two types of tables available(current and archival) and structure of the current and archival tables are exactly similar and data will be moved for current table to archival table over the period of time for performance reasons. I'm having repository classes to retrieve the data from current and archival table separately and Pagination is also implemented for repositories.
Now I got a requirement to fetch the eligible records from both tables based on criteria and apply pagination at single shot. Is it possible with Spring Data JPA
You can keep the latest version in both tables and when you search for data you just do a regular search.
Another option would be to create a view over the two tables.
I also think Hibernate Envers was able to do that though I never tried it.
I have integrated hibernate envers in spring boot. Now my requirement is to have old value also for the particular column when value changed in *_AUD tables. However I cant see any feature available in Hibernate Envers plugin.
Please suggest.
Thanks
Unfortunately what you are looking to do just isn't someting supported.
It's one thing to think of an entity and needing to store basic type values such as strings or numeric data and have its old/new value represented by two columns in the audit table; however when you move beyond basic entity mappings to ones where you have relationships between entity types or collections; you begin to see that trying to store old/new data in the same row just isn't efficient and in some cases feasible.
That said, you can still read the audit history and deduce these old/new values using a variety of ways that include the Envers Query API, Debezium, or even basic database triggers.
In our application, we have a requirement to audit "viewed by" events. Currently, we implemented this functionality by using an Audit table and manually logging it to the table during "GET" calls. I am trying to understand how to accomplish this in Javers.
In our current application, to find our changes, we use hibernate interceptor and manually add the changes to the audit table.
I thought that the easiest way to accomplish the "viewed" audit functionality in Javers is to add a "viewedBy" field to the entity being audited and manually update it in "GET" calls. But I am concerned about this approach as each time, there is a view, we are changing the version of the object(by physically updating it) and the state is being saved to the jv_snapshot table.
I expect that the viewed by audits will be part of javers.findChanges() method, so that the changes are tracked in a chronological order and also possibly be paginated.
In Hibernate Envers it is possible to have a separate audit table. Similarly, is it possible to log into tables other than the entity’s table using Spring Data JPA auditing?
The auditing feature of Spring Data JPA just fills attributes in the entity you are persisting. How and where these attributes get persisted is controlled by you JPA implementation and of course your database.
JPA offers #SecondaryTable to map fields to a second table.
If this isn't flexible enough for you, you can always employ database tools to achieve the effect by mapping the entity to a view which via triggers distributes the data however you want.
I wonder how to retrieve deleted record that audited with Javers, as I know how to retrieve version changelog but when the record deleted from database table, there is no ID to retrieve audit record.
There is no dedicated filter for selecting deleted objects snapshots.
You can use general purpose queries like byClass() and filter selected snapshots by SnapshotType on your own, although it will not be very efficient.
New SnapshotType filter can be easily implemented in JaVers JQL, consider contributing a PR.