So we use hbm2java pretty intensively right now since it helps us understand the mappings quite a lot.
But we're getting to the point where we want to add domain behavior to our domain objects.
It's been suggested that the way to do this is to use <meta extends="AbstractSuperclass">, like so. Let's say I have an Order class containing LineItems, and I want to write a calculateDiscount method on it which looks at the LineItems and uses some business policy to calculate the order discount. It seems like the only thing I can do with <meta extends> is something like this:
I have something like an Order entity which has a bunch of LineItems (you know, the usual example) and I want to write a totalPrice method? This wouldn't work:
// class written by me with domain logic method
abstract class OrderSuperclass {
public float calculateDiscount () {
Enumeration lineItems = this.getLineItems();
// ... add up all the lineItem.getPrice()s and calculate a discount amount ...
return discount;
}
}
// class generated by hbm2java with <meta superclass="OrderSuperclass"/>
public class Order extends OrderSuperclass {
public List getLineItems() { return lineItems; }
//...
}
But this obviously doesn't work, since OrderSuperclass won't compile, since it itself has no getLineItems() method! Do I need to add "public abstract getLineItems();" to OrderSuperclass? If so, doesn't that mean that we need to push a lot of generated code up to the superclass?
What are the best practices here? How do other people add behavior to hbm2java-generated classes? Seems like the strategies are:
1) Use hbm2java exclusively, and keep all behavior in external classes. (Seems like this breaks encapsulation in your domain model, and makes it hard to see where business rules are implemented.)
2) Use hbm2java only as a starting point, and put lots of logic in your Hibernated business entities. (Seems like this retains encapsulation, but makes changing your data model more cumbersome.)
Is there a third way? If not, what do most people do in this situation?
Cheers!
Rob
|