-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Implicitly deleting aggregate root
PostPosted: Sat Jul 25, 2009 12:59 pm 
Newbie

Joined: Sat Jul 25, 2009 12:44 pm
Posts: 3
Hi guys,

Using Hibernate Core 3.3, Hibernate Annotations 3.4 and Hibernate EntityManager 3.4.

We have a WorkOrder entity which has a @OneToMany association with a WorkOrderDetail entity. WorkOrder is the root of this aggregate, no other entities reference WorkOrders. Throughout its life, a WorkOrder will own a variable number of WorkOrderDetails. When a WorkOrder owns no more WorkOrderDetails, it should be deleted from the database.

The way we see it, there are 2 clean options to implement this rule:
1. Creating an EntityListener for WorkOrder that would, within a @PostUpdate callback, delete the WorkOrder at the proper time
2. Creating a service for removing WorkOrderDetails from a WorkOrder, which would procede with deletion at the proper time

From what I've read, EntityListeners are not allowed to call an EntityManager in its callback methods. Option 1 is therefore ruled out.

Option 2 doesn't seem very object-oriented. Clients should be able to just call workOrder.removeWorkOrderDetail(wod) and not go through a specialized service for this.

We would like to avoid option 2, but it seems to be the only available option. How would you guys go about implementing this in a clean, object-oriented fashion?

Cheers,
Spiff


Top
 Profile  
 
 Post subject: Re: Implicitly deleting aggregate root
PostPosted: Mon Jul 27, 2009 1:46 am 
Newbie

Joined: Fri Jul 24, 2009 11:06 am
Posts: 8
workOrder.removeWorkOrderDetail(wod) could look for zero work order details, in which case, it calls workOrderRepository.delete(this).


Top
 Profile  
 
 Post subject: Re: Implicitly deleting aggregate root
PostPosted: Mon Jul 27, 2009 8:11 am 
Newbie

Joined: Sat Jul 25, 2009 12:44 pm
Posts: 3
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.