-->
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.  [ 9 posts ] 
Author Message
 Post subject: Hibernate Business Objects?
PostPosted: Thu Nov 17, 2005 7:13 pm 
Newbie

Joined: Thu Nov 17, 2005 7:06 pm
Posts: 2
Is it possible to extend the (often auto-generated) persistence classes defined in the hbm.xml file to add custom business logic? What is the recommended technique for doing this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 11:22 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
It's pretty much as you just described. You'll want the hand written classes to extend the generated ones, otherwise you get problems when it comes time to regenerate. Typically, you do stuff like this ...

public String getAddressFormatted(){
return this.address + ", " + this.state.getCode() ;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 1:31 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
You might get into trouble if you have the handwritten class extend the generated class because what hibernate hands you after an operation is the generated object and not the handwritten object.
You could do it the other way (Have the generated one extend the handwritten one) by using the meta attribute in ur hbm.xml
refer to
http://www.hibernate.org/hib_docs/refer ... guide-s2-2
for more information on extending from a handwritten class.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 2:03 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
"FROM ChildOfGenerated" will return a list of all "Generated"s .


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:19 pm 
Newbie

Joined: Thu Nov 17, 2005 7:06 pm
Posts: 2
Hmmm... I'm not too thrilled about the idea of generated classes subclassing from my business objects. For one thing it seems like you'd run into circular dependency classloader issues, no? (Assuming the generated classes & business object classes live in different jars.)

For now I'm just "wrapping" my generated classes with my business classes, basically using aggregation rather than inheritance. This is less than ideal though because often I'd like to access methods on the generated classes directly.

This is such a common problem that I'm sure somebody has come up with a clever solution, but so far I haven't heard it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
living in different jars is not a problem and there is no cycles in the code.

just use the generated-class meta attribute to control it.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 3:40 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
dennisbyrne wrote:
public String getAddressFormatted(){
return this.address + ", " + this.state.getCode() ;
}


It must be better to implement business logic outside data model:

public String getAddressFormatted(MyData data){
return data.address + ", " + data.state.getCode() ;
}

This kind of business logic needs some context like Locale,User, some not persistent data or additional data access method calls.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 4:14 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Locale usually applies to the label of a property, "address" (english) vs. "adresse" (deutsche) . The value of the property, "123 A St. Anchorage Alaska, USA", is going to appear the same for Americans as it does for Germans.

baliukas - see the toString() methods for classes in org.hibernate.auction for my thoughts ... this saves a JSP author a lot of JSTL/EL lines of code.

Nobody here is saying it is OK to put methods like isQualifiedForLoan() or givePromotionTo() inside of Person. And if that's what the original poster had in mind, then yes, listen to baliukas.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 9:58 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
This issue is typical in my practice, I use method overloading and request scope instance to solve it:

Code:
class Format {

  Format(  Locale locale, ... ){
    ...
  }

String format( Date date )

String format( Address date )
  ....
 


}


Ceparation of concerns is usefull for parsing or data access too.


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