Getting old values โ€‹โ€‹of updated domain properties in grails

I am trying to implement a beforUpdate event in grails domain classes. I need to audit the journal for both old and new Domains attribute values. I see that we can use the isDirty check or use Domain.dirtyPropertyNames , which return a list of properties that are dirty in the domain. and getPersistentValue gets the old value in the table, so I can have both values.

To implement this, I will use the beforUpdate event in the domain class and call the logging service there, passing it the user domain ID. Now, using this identifier, I can get a user instance in the service, and then check if any fields are dirty using the above method? or do I need to register an audit when I really do an update in the UserController def update?

What is the best approach?

I want to confirm if this is right.
Also, what else do I need to take care of, for example:
1) if the attributes are object object references, and not simple types.
2) any other things that I need to take care not to flush the hibernation session, thinking about implementing this when calling a service from a domain class.
Respectfully,
Priyank

Edit: I tried this in the beforeUpdate event in the domain of the user that I want the audit log to update activity.

 def beforeUpdate = { GraauditService service = AH.getApplication().getMainContext().getBean(''graauditService) service.saveUserUpdateEntry(this.id); // id property of User domain... } 

In the method in the service, I do:

 def saveUserUpdateEntry(Long id){ User grauser = User.get(id); println ("user="+ grauser) println "Dirty Properties -: ${grauser.dirtyPropertyNames}" println "Changed value for firstName = -: ${ grauser.firstName}" println "Database value for firstName = -: ${ grauser.getPersistentValue('firstName')}" } 

I am trying to update from the user interface for email, first name, last name and get the following on the console:

 user=com.gra.register.User : 1 Dirty Properties -: [eMail, firstName, lastName] Changed value for firstName = -: sefser Database value for firstName = -: administer user=com.gra.register.User : 1 Dirty Properties -: [] Changed value for firstName = -: sefser Database value for firstName = -: sefser possible nonthreadsafe access to session 

I can not know:
1) Why do I get 2 sets ... is this an event called twice once before commit and once after commit ...?
2) how to remove or handle a Hibernate exception (tried to use with a new session in a function, but no difference
Thanks at Advance ..

+4
source share
1 answer

Instead of using GORM event handlers to audit the audit log, use the audit audit plugin . This will take away a lot of pain from you.

Hope this helps.

or

If you want much finer control over what you are doing, you should consider using the Hibernate EmptyInterceptor subclass. This will serve you.

  • will give you much finer control over what and how you keep an audit trail.
  • Put all your logic for auditing in one place, which helps you save the code.

Click here to see the API for EmptyInterceptor.

Note Hibernate does not send any implementations in this class, nor does it provide any subclass that can provide you with default behavior. So you have to write a custom implementation.

0
source

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


All Articles