0

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.

Tuan
  • 1,476
  • 13
  • 23

1 Answers1

1

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.

andrus_a
  • 2,528
  • 1
  • 16
  • 10
  • Andrus, I'm setting the Employee object into the Assignment object, the Employee object is the one that had its Prepersist call back triggered. I've tried setting them to a localContext to deal with them being on two different contexts. On commitChanges of the localContext, it would still trigger the Employee's callback and goes into StackOverflowError due to infinite recursion. How do you do commitChanges() without triggering the call back from Employee object? – Tuan Jul 23 '15 at 21:32
  • In a situation where you are assembling an "aggregate" object made of other persistent objects, I suggest taking it out of callbacks completely and into some factory class. Now that you explained what you are trying to do, this answer makes much more sense that what I posted originally. So I'll amend the answer – andrus_a Jul 27 '15 at 08:55