views:

26

answers:

1

Given that you have a lot of domain objects, that all interact with one another, it would be very useful to know which objects have changed in a particular transaction.

Is this possible ? I would like to essentially do this :

public void someBusinessLogicMethod(someparams) {
  Session s = getSession();
  Transaction tr = s.beginTransaction()

  domainObject = s.load(...)
  domainObject.setSomethingOrOther(...);
  domainObject.getSomeLink().setSomethingElse(...);
  callSomeOtherBusinessLogicMethod();

  tr.commit();

  /* at this point many objects have changed, Hibernate knows which ones */

  for (Object s : tr.getAffectedObjects(?)) {
    ....
  }
}

Does this exist ?

A: 

Assuming you want to do something like create audit entries for all the changes, you could use a Hibernate Listener or an Interceptor. If you hook the listener/interceptor at the right moment (e.g. onFlushDirty), you have access to the objects and properties that have changed.

More info: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/events.html

Hope this helps.

Sergio