Search code examples
apache-cayenne

Trigger callback method from another callback method


I have a PrePersist callback that creates Assignment objects. I need to trigger callbacks to let me know when these Assignment objects are inserted into the database. PostPersist callbacks for Assignments are not being triggered this way. The only callback that got triggered is the PostAdd, which isn't helpful to me. How do I go about doing this? Thanks.


Solution

  • The rules of thumb for persistent object events are:

    • callbacks on objects are for the logic related to the object itself (e.g. setting creating timestamp, resetting a non-persistent property, etc.)
    • listeners (another form of Cayenne event processors) are for audit and workflow tasks that need to happen when an object is committed.

    You have a different situation which is assembling an "aggregate" object made of other persistent objects. So I suggest taking this logic out of callbacks completely and into a factory class that knows how to create and connect objects together.