Whenever a specific change occurs to one of my domain objects I want to be able to make another change. As an example, I have an object that has a set of states that are a part of a relationship with other objects. However, I also need to maintain a state that takes into account all the states for the relationships.
Currently, I am just loading the object and generating the state on the fly. What I would like to do is update the state whenever one of the relationship states change. So far I can think of two places to do this:
1. When the state for the relationship is changed, also update consolidated state.
2. Use an interceptor. When save() is called, make the update.
Option 1 doesn't seem optimal, because this value will be set whenever I get the object back from Hibernate. For read only operations, I don't want to make a change to the database.
Option 2 seems like a better approach. However, my understanding is that this would only be called after a save(), so I might have a stale consolidated state for a period of time.
Is there yet a better approach? At worst, I can always call save in the DAO when it makes an update to the relationship state for the domain object. If there is a better solution I'd love to use it though :).
|