Displaying audit history in hibernate envers - hibernate-envers

I am new to hibernate envers.I have successfully created audit tables for each entities.but i don know how to read these history for each entity.I want to display this history in a jsp.I want to read all the history for that particular entity.any body know how it can be done.

Take a look at the AuditReader class:
http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/envers/AuditReader.html
And the Envers tutorial:
http://docs.jboss.org/hibernate/orm/4.1/quickstart/en-US/html/ch05.html

Related

How to implement audit on a table using Hibernate?

I'm new to Hibernate and I want to audit all changes made to a table, to get logged into its Audit table.
In other words, any INSERT, UPDATE, DELETE on TEST table must be inserted into the TEST_AUD table.
I read a few articles on using dynamic audit tables that are created, but I cannot use it. The audit table must not be automatically generated.
I did not get a satisfactory article or solution to implement auditing using entities and Hibernate in SpringBoot?
Can someone please suggest a good article or provide an example?
Thanks!
You should use hibernate envers: https://hibernate.org/orm/envers/
set hibernate.hbm2ddl.auto to create, create-drop or update
or use org.hibernate.tool.EnversSchemaGenerator
You can audit entity or properties. If you entity name is TEST, the TEST_AUD will be automatically created.
You can also follow this article https://www.baeldung.com/database-auditing-jpa for more informations.

How to keep a history of edit of Entities in a JPA application

A JavaEE and JPA application need to keep a record of all the changes made by the user.
Currently, for all the entities, there are fields to record createdBy and lastEditedBy properties. Yet, the requirement of recording all edits is not possible with those properties.
What is the best way to record the history of all edits for a particular entity?
I do not use Spring.
You can use Javers which is db and framework agnostic tool for maintaining operation history.
There are two big differences between JaVers and Envers:
Envers is the Hibernate plugin. It has good integration with Hibernate
but you can use it only with traditional SQL databases. If you chose
NoSQL database or SQL but with another persistence framework (for
example JOOQ) — Envers is not an option.
On the contrary, JaVers can be used with any kind of database and any
kind of persistence framework. For now, JaVers comes with repository
implementations for MongoDB and popular SQL databases. Other databases
(like Cassandra, Elastic) might be added in the future.
Envers’ audit model is table-oriented. You can think about Envers as
the tool for versioning database records.
JaVers’ audit model is object-oriented. It’s all about objects’
Snapshots. JaVers saves them to the single table (or the collection in
Mongo) as JSON documents with unified structure.
You can also achieve this using triggers and storing object differences.
Edit:
JaversAuditableAspect for any kind of repository.
It defines the pointcut on any method annotated with the method-level #JaversAuditable annotation. Choose it if you have repositories that are not managed by Spring Data.
#Bean public JaversAuditableAspect javersAuditableAspect() { return new JaversAuditableAspect(javers(), authorProvider(), commitPropertiesProvider()); }
You can use Hibernate's Envers to audit your entities. It allow you to keep track of ALL changes made to entities - even deleted ones. Most probably you are already using Hibernates (as JPA provider) so integration should be a no problem.
https://hibernate.org/orm/envers/

How do I create a read-only entity?

I have a web application using J2EE + Spring and a MySQL database. I need one entity which will be read-only. I have one main table with products, and they are only to read. There should be no insertion of new records and no updates currently.
The entity class should only read data and pass the entities forward (other entities are tables like order, shipments etc.).
Is there any solution for this? Does anyone have the same issue? Thanks for the help.
If you don't change an object, it will never be updated.
If you are using EclipseLink you can use the #ReadOnly annotation to mark something as read-only.

Can I use Hibernate Envers if I just want to keep deleted Entities around?

Right now we don't delete entities, but set a flag to "inactive" in the table (and filter these entities out for normal operations). Someone pointed me to Hibernate Envers, but it looks a little bit like overkill to me. My questions are:
Can we use Envers to perform our mechanism (active/inactive flag)?
If not, can Envers store a copy of a deleted entity in an archive table, but don't do any versioning / auditing stuff?
Are there lightweight alternatives for this task?
You could use Envers here, by extending the audit listener and ignoring insert/update events, however I agree that's an overkill.
Simply using an active flag with a dedicated DAO method or writing a simple Hibernate event listener should be much better suited for this task.

Windows Workflow Persistence data (VS 2010 RC / .NET 4.0)

I have started working with Windows Workflow recently (the VS2010 RC / .NET 4.0 version) and am stuggling to get to grips with the SQL persistence functionality.
I have managed to attach persistence to my WorkflowServiceHost via an SqlWorkflowInstanceStoreBehavior object and in my database there are rows appearing in the [System.Activities.DurableInstancing].[InstancesTable] table.
However, I don't know how to make sense of any of this data (it seems as though quite a few columns are in binary format). How can I store custom data regarding my workflow in this? How do I retreive this from the table for MI style reporting?
I can't seem to find any info on the web regarding storing custom data (and then retrieving it again) - please help :)
Many thanks in advance!
The data you see is all use by the workflow persistence system and not really suitable for your own consumption. If you want to query on your own data you need to use a mechanism called property promotion that stores the data in a queryable format using the InstancePromotedProperties table. Basically you need to implement a PersistenceParticipant and overwrite the CollectValues() function to add the values to one of the collections.
See here for more details.