How to implement audit on a table using Hibernate? - postgresql

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.

Related

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.

JPA table capitalization inconsistent

We're using a very basic JPA implementation that should create tables consistently from our models.
I believe we're using EclipseLink or TopLink (whichever one is default with the latest Netbeans/Glassfish). The problem is, the tables are created with inconsistent capitaliztion and with the columns out of order. For me, It creates the "User" table as "user", and for other members of my team it creates "USER".
I've tried using the #Table annotation (#Table(name="USer")), but it doesn't work.
How do we get EclipseLink to generate consistent table names? Frankly this seems like a rather amateurish mistake for a framework like this.
Sub-question : the reason this is a problem is because EclipseLink by default has no default way of managing schema/data migrations, as far as I know of. The way we're handling it is by writing a bunch of INSERT INTO's to bootstrap the objects we need in our database, and drop-and-recreating the tables every time the schema changes. I know this is not the best practice for propagating schema changes -- does anyone know how this is typically handled in a standard JPA implementation?
Thanks.
By default EclipseLink uses all upper case for the table name, the class User would be USER.
If you specify an #Table annotation with name="USer", then the table will be created as "USer".
Perhaps you are using your own scripts to create the tables, or you database is changing the case based on the OS or its own settings. What database are you using?
If you enable logging in EclipseLink, it will show the exact DDL that it is executing (if it is executing DDL).
In EclipseLink 2.4 there is also a "create-or-extend-tables" DDL generation option to alter existing tables.
We never found any good answer for this. Luckily, we found a workaround for the ways we were using to update the table, which didn't care about capitalization.

Displaying audit history in 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

EF 4.3.1 code first table index mapping or creation

I've been searchin around a lot for a way to create, or configure a specific index on a table from EF code first, but really can't find a thing. mapping fluent Api doesn't seem to expose any means to do this. What am I missing?
You can add index creation either to custom database intitializer as mentioned in linked answer or you can use code based migrations. Migrations provide support for index creation.

Envers audit entities

What is the better way to get list of audited entites?
It is possible to use REVCHANGES table and look up all audited entytoes from this table but I belive that this is not best solution.
Thanks,
Volodymyr
Given Hibernate's Configuration object, you can call AuditConfiguration.getFor(cfg), and then the EntitiesConfigurations object to check if an entity is audited or not.
Note though that this is not an official API.