Easy way to save audit core data for domain objects?

My application uses large trees of domain objects, and for most of these objects I would like to save some basic information (updated, changed time, etc.). I have already added these properties and columns to my application.

I was going to encode the settings for these values ​​in all the different constructors, etc., when it occurred to me that the persistence layer should handle this transparently.

But how?

I could do this from within my DAOs, but how to handle objects that are saved using cascading save? Is there a way to intercept the persist () method?

What is a good way to realize this opportunity?

+4
source share
2 answers

If you intend to use JPA for auditing, you can rely on PrePersist and PreUpdate callback annotations. The JPA WikiBook has one such example . In this case, it is very important to have a mapped superclass, otherwise you will continue to do this through the "user" space code.

Another way to do this is to use triggers (um) for tables. However, this affects performance. The Openbravo ERP project seems to use this approach to create an audit trail.

Update . It is recommended that audit functionality be distinguished from another class other than the superclass of the domain model. JPA event listeners can help you do the same. A good example is here .

+5
source

You might want to take a look at Hibernate Envers.

+6
source

Source: https://habr.com/ru/post/1314912/


All Articles