In various tables, we have several properies which MUST be entered in order to insert or update a row. For instance we have a property called log_user which tells us who the last person to make a change was.
Setting the property to not-null does not solve this as the prior value in the table is irrelevant. What we care about is that when a change is made, that the log_user field is set. Now the value could be the same as the old value (if the user is the same) so you can't simply just look for NO change in the value either.
The goal is to enforce this across most classes that are persisted in the system and detect when a developer missed something.
I can see two ways to address this:
1) After hibernate loads the object, reset the values to null. Then use a non-null property constraint which will complain when you store without updating the property. BUT, it does not look like there is a hook for an interceptor to be called after properties are loaded into an object (presumably because of lazy loading?).
This looks like the cleanest, most efficient method. Is there a way to do this?
2) Create a separate method to set the value of the member variable in the object which also sets a changed flag of sorts. This separate method is meant for developers versus the with original setter is only for hibernate only. Then create an interceptor which throws an exception if the flag is not set (or clears the flag so that subsequent reuse of the object will have the same behavior as new object). There might be some issues with inserts followed by updates here....
Any other ideas?
I know that a seperate log table would be a solution, but we don't have that luxury
|