Member Menu
 
 Monthly JBoss newsletter:
 
Hibernate Books
CaveatEmptor

Codegeneration with Root Persistent Class

NB: This only works for Hibernate2

In Root Persistent Class is described how an base class for ones persistent class could look like. This is all great, but what if you (like me) want to use the codegenerator ? How do you inform the codegenerator that your class (or all classes) should extend that base class ? Well - Hibernate2's hbm2java is your friend:

<class name="Person">
  <meta attribute="extends">Persistent</meta>
  ...
  ...
 </class>

The above "snippet" hbm2java will parse and generate something like this:

public class Person extends Persistent {
 ....
 }

And know you can just define your properties in your hbm.xml file and hbm2java will do all the boring work for you :)

But what about subclasses in hbm.xml ?

They are no problem. hbm2java simply ignores the "extends" meta tag for any <subclass> or <joined-subclass>.

Do I need to specify the <meta> tag for each <class> ?

If you are lazy (as me) and do not want to put that <meta> attribute on each and every class in your hbm.xml file you can do one of two things:

Insert an <meta> in top of the hbm.xml file as follows:

<hibernate-mapping>
    <meta attribute="extends">Persistent</meta>
   ...

This will then work as the default for that hbm.xml file and it can (of course) be overriden if you wish to for certain classes. Just add a <meta> tag for those classes as described above.

The second option is to put the <meta> tag in a config.xml which can then be provided to hbm2java and then the <meta> tag will be used as an default for ALL your hbm.xml files.

Happy code generation!


  NEW COMMENT

hbm2java does not check existence of properties in base class! 03 Feb 2004, 23:40 eepstein
So if you add a <version name="version" column="update_count" /> tag 
to your class's hibernate mapping, which the Persistent base class has 
a version property, the generated class will hide the base class's 
property and accessors!!

What this means is that if you want to use the pattern referenced on 
this page you should declare the version (and id) methods as abstract 
and then use a consistent naming convention in the generated classes 
for those properties.

Also, for generality, it's best not to specify the type of the id 
property in such a limited way.
 
Using base and sub classes per table 03 Nov 2004, 10:13 omisch
The second mentioned option does not work. I posted a question in the 
tools forum, and got this answer. The first approach (one tag in one 
hbm file for all classes) does not work either when you create the hbm 
files with middlegen, because middlegen produces one file per class. 
Is there any othe way?
 
The meta extends also works for hibernate3 11 Mar 2006, 14:52 sujitpal
FWIW, the first approach (setting the meta (at) extend in your  (dot) hbm.xml file)
also works with the hibernate3 code generator. I am using these:
hibernate3.jar
hibernate-tools-3.1.0.b2.jar
 
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]