I have a Hibernate entity object MyObject
with several attributes and a service MyObjectService
containing business logic dealing with MyObject
. I also have a view model dealing with user input/output which uses MyOjectService
. This service contains method updateMyObject(MyObject o)
which is called by the view model when user changes some attributes, and it saves these changes into the database.
When some (not all) attributes of MyObject
are updated, I need to notify an external system. I'm trying to figure out a best way of implementing this functionality. These are the solutions I came up with so far:
Method
MyObjectService.update(MyObject o)
would get a second boolean parameter saying whether to send the notification to the external system. This is the simplest solution, but It's probably not good to let the view model (which calls the method) decide whether to send the notification or not. I'd rather have this decision in the service.The view model would keep a copy of the original state of the object along with the modified state. Method
update
would then have 2 arguments (before and after) and it would check whether the attributes which need to be notified of are changed. This would result in a pretty ugly condition likeif (before.attr1 != after.attr1 or before.attr2 != after.attr2 or ... )
containing a large number of attributes which can grow in the future.Have a separate update method in
MyObjectService
for each attribute ofMyObject
. This way, each method would decide whether the notification has to be sent or not. This is probably the cleanest way, but I'd have to have a large number of update methods (1 for each attribute).
So each of these solutions has its disadvantages. Which one would you use? Or do you have a different idea how to do this? Is there some way I could use Hibernate to help me with this? (I tried to find out whether I could use Hibernate to find out which attributes are modified, but I didn't find a satisfactory solution).