Jun 19th, 2012, 11:46 PM
Tracking User changing Data in DB
I am using DAOs to update my DB and I'd like to store the Username every time the user changes data in the DB.
I'd like to avoid passing the Username to the DAO layer when update-Method is called, as for example:
Is there a way to use DI to inject the user via a setter method. Normally I inject static values, which are configured in XML-File but in that case I'd have to inject values (objects) that change during runtime.
Would be great to get any hint...
Jun 20th, 2012, 12:15 AM
Read up on writing Aspects with Spring AOP. In a nutshell, you can intercept various methods and log some information (or do anything you like) about what's going on (before, during, and/or after the action). I think this may be ideal for you. It keeps the auditing code completely out of your business logic.
Jun 20th, 2012, 01:00 AM
You could use AOP but it actually depends on your persistence layer. Do you use plain JDBC, JPA or Hibernate. In the case of JPA you might want to checkout spring-data as this supports that out of the box for hibernate you could write your own EntityListener.
Jun 20th, 2012, 01:30 AM
thanks for that hint. I knew that I could use AOP for that, but in my case, I'd like to store the username together with the business data, for example:
Originally Posted by JeffH
Customer Birthdate Changed ChangedBy
Williams 05.02.1980 10.10.2011 Admin
So I think, I need the User Info in the DAO and not somewhere else.
Jun 20th, 2012, 01:33 AM
I'll check out spring-data. Actually i use plain JDBC.
I forgot to mention that I only have 1 DB User so I can not use DB mechanisms to achieve it.
Tags for this Thread