-->
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.  [ 5 posts ] 
Author Message
 Post subject: Newbie: implementing cascading delete for linked entities?
PostPosted: Mon Jul 09, 2007 7:16 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
I have a question about the validity of using cascading delete Hibernate definitions.

My example involves two entities 'Customer' and 'Order'. My Hibernate design envisages these as being two discrete hibernate mappings. Although a customer is associated with an order (the order table will include a 'customer ID' foreign key column), the orders are not defined at the same or place as customers are created and maintained.

Hence...

Code:
public class Customer {
   private int CustomerID;
   private String CustomerName;
   ...
   ...
}

public class Order {
   private int OrderID;
   private String OrderNumber;
   private double OrderAmount;
   private int CustomerID;
   ...
   ...
}


As the Customer class is potentially quite large neither does it make sense for the customer class to be added as a property to the Order class.
Thus, I have two separate Hibernate mappings for one each class, with no one-to-many association included in the Customer mapping.

The downside of this approach is that I lose the cascading delete facility by having the association between customer and order.

To address this is it valid to create a second class that descends from 'Customer' with its own Hibernate mapping that overlays the same customer table. THis new mapping would be used solely as a vehicle for delete operations?

Like..
Code:
public class CustomerPlus extends Customer {
   private Set<Order> fOrders = new HashSet<Order>()
   ...
   ...
}


In the Hibernate mapping for 'CustomerPlus' I'd add a lazy load one to many association with a cascading delete. For all customer 'delete' operations in the DAO tier Java class I'd load the CustomerPlus class and use it as a vehicle to enact deletion of the related orders.

Does this make sense? Thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 09, 2007 8:59 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Yes, it's reasonable approach. You can create separate CustomerLightWeight class with its own mapping and use it in one-to-many association and for delete operations. CustomerLightWeight class doesn't have to be subclass of Customer and vice versa. CustomerLightWeight and Customer must be mapped as different entities.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 09, 2007 8:59 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Quote:
Yes, it's reasonable approach. You can create separate CustomerLightWeight class with its own mapping and use it in one-to-many association and for delete operations. CustomerLightWeight class doesn't have to be subclass of Customer and vice versa. CustomerLightWeight and Customer must be mapped as different entities.



Sorry for the second post of the same message, i've had problems with connection and with posting. Could moderator delete second post, please? I can't do it via interface.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 10, 2007 2:24 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
Can I setup my 'CustomerPlus' hibernate mapping as a subclass in the same mapping file, or does it have to be in a sep[arate mapping file?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 10, 2007 8:03 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
I think, you had better map CustomerLightWeight (or CustomerPlus) and Customer as completely different entities, otherwise you'd need to introduce discriminator column or additional table in your schema (see "Mapping class inheritance" in Hibernate). You could, however, map two different entities in the same mapping file (if they belong to the same db schema).

_________________
Best Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.