-->
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: How to use <meta superclass="..."> effective
PostPosted: Fri Jan 30, 2004 8:01 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 01, 2004 8:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Use the meta tage <meta attribute="generated-class">classname</meta>
which gets replaced each time it is generated and it gets inherited by the domain object.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 12:22 am 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
I see. So my hbm.xml has something like

<class classname="my.DomainObject">
<meta attribute="generated-class">my.gencode.DomainObject</meta>
...
</class>

and then I write

package my;
class DomainObject extends my.gencode.DomainObject {
public DomainObject () {}
// add domain methods here....
}

So then Hibernate still creates my.DomainObjects which have all the right properties?

That is slick.

Just like Hibernate :-)
Thanks very much!
Cheers!
Rob


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.