Update father on interceptor
-
21-09-2019 - |
Question
My model:
public class Father {
Set<Son> sons = new HashSet<Son>();
String name = null;
Date lastSonModifyDate = null;
// ... other fields and setters/getters
}
public class Son {
Father father = null;
String name = null;
Date lastModifyDate = null;
// ... other fields and setters/getters
}
Use case:
- There is in DB a
Father
object with aSon
object associated (bidir). - Load from DB father.
- Update name field for father.
- Update name field for son.
- Persist father.
My interceptor first detects father updates (onFlushDirty). Then executes the onFlushDirty for the son. In this case, I update son.lastModifyDate and also father.lastSonModifyDate.
When execution ends, all updates are persisted except father.lastSonModifyDate. I think this is because father is in session and has been updated before son, so this entity overrides the changes done in onFlushDirty method for the son entity.
How could I achieve my mark (set father's lastSonModifyDate from son interceptor)?
Thanks.
Solution
You can't. onFlushDirty() is invoked for collection elements after it has been invoked for owner and update action (if any) has already been scheduled.
Is there any reason why you can't do all of the above in your DAO instead of relying on interceptors? Or on the database level (mapping both lastModifyDate
properties as generated)?
OTHER TIPS
you need to override onCollectionUpdate for entities that has cascading option. this is called before its being scheduled.
cheers~