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.  [ 2 posts ] 
Author Message
 Post subject: Best Practices for generating classes from hbm.xml
PostPosted: Tue Jun 13, 2006 3:30 pm 
Newbie

Joined: Tue Jun 13, 2006 3:24 pm
Posts: 5
I am working on a project where we are using the *.hbm.xml as the basis for both generating the classes and the schema and we have hit a point where we really want to add behavior to the generated (persistent) classes.

Is there a best practice for this that either allows us to save modifications to the generated classes or to somehow use one class mapping at class generation time and another (the subclass with added behavior) at run time?

Since we have not yet solidified the object model, we don't want to blindly edit the generated classes.

Alternately, should we dump the *.hbm.xml-centric approach and move to generating that file from the java class?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 6:59 am 
Newbie

Joined: Tue Jun 13, 2006 4:26 am
Posts: 3
Location: Bristol
Ted,

You can specify a base class to generate, which you can then extend and add your business logic to. All hibernate access is then done through your base class.

Take a look at this page for more info: http://www.onjava.com/pub/a/onjava/2005 ... ntent=text

For your convenience I've c+p'd the important bits here:

Using a Base Class
You can also use the generated-class meta attribute to define a base class, which will be generated by hbm2java, leaving you free to place your business logic in a subclass of this generated class. For example, using this technique for the Country class could be done as follows:


<class name="Country" table="COUNTRY" dynamic-update="true">
<meta attribute="implement-equals">true</meta>
<meta attribute="generated-class">CountryBase</meta>
<meta attribute="scope-field">protected</meta>

<id name="id" type="long" unsaved-value="null" >
<column name="cn_id" not-null="true"/>
<generator class="increment"/>
</id>

<property column="cn_code" name="code" type="string"/>
<property column="cn_name" name="name" type="string"/>

<set name="airports" >
<key column="cn_id"/>
<one-to-many class="Airport"/>
</set>
</class>

hbm2java will generate the CountryBase class, containing all the attributes, getters, setters, etc. described by the mapping file. Then you are free to place your business logic in the derived class called Country, which will be used and instantiated by Hibernate; for example:


public class Country extends CountryBase {
/**
* Add an airport to this country
*/
public void addAirport(Airport airport) {
airport.setCountry(this);
if (getAirports() == null) {
setAirports(new java.util.HashSet());
}
getAirports().add(airport);
}
}


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