goody_guy2002 wrote:
workOrder.removeWorkOrderDetail(wod) could look for zero work order details, in which case, it calls workOrderRepository.delete(this).
Thanks for your suggestion. While this would work, I see a few drawbacks to this approach:
1. My WorkOrder entity now needs to be aware of the WorkOrderRepository, which I tend to avoid so as to not mix domain logic and persistence logic
2. If the previous point cannot be avoided, WorkOrder needs to call some DI code (such as a Spring ApplicationContext) to retrieve the WorkOrderRepository, or AOP needs to be introduced
3. If the client intended to call workOrder.addWorkOrderDetail(wod) after the previous workOrder.removeWorkOrderDetail(wod), it would end up with a removed instance. Come to think of it, this disadvantage applies to my original second solution.
What's needed is a mechanism that, at the end of the use case, before the transaction commit, checks this condition and potentially deletes the WorkOrder. Clearly the @Pre/PostUpdate lifecycle callbacks would be ideal to accomplish this, and the "no call to EntityManager" restriction is all the more restrictive.