In Hibernate 4.0 (maybe earlier), you can use nested elements if you are using the table-per-hierarchy model.
Example class structure:
Word (has property "text", string)
- Subject (adds property "plural", boolean)
- Pronoun (adds property "masculine", boolean)
- ProperNoun (adds property "person", boolean)
- Verb (adds property "past_tense", boolean)
Example Hibernate mapping XML:
<class name="test.Word" table="words">
<id name="id"><generator class="native"/></id>
<discriminator column="word_type" type="string"/>
<property name="text"/>
<subclass name="test.Subject">
<property name="plural"/>
<subclass name="test.ProperNoun">
<property name="person"/>
</subclass>
<subclass name="test.Pronoun">
<property name="masculine"/>
</subclass>
</subclass>
<subclass name="test.Verb">
<property name="past_tense"/>
</subclass>
</class>
Then, after running the following code (I've left out the class code, it's very basic and honestly only the object types themselves are significant for this example):
HibernateUtil.beginTransaction();
HibernateUtil.getCurrentSession().persist(new Word("blue"));
HibernateUtil.getCurrentSession().persist(new Subject("chairs", true));
HibernateUtil.getCurrentSession().persist(new ProperNoun("James", true));
HibernateUtil.getCurrentSession().persist(new Pronoun("he", false, true));
HibernateUtil.getCurrentSession().persist(new Verb("sat", true));
HibernateUtil.commitTransaction();
The database ends up containing this information:
id, word_type, text, plural, person, masculine, past_tense,
1, test.Word, blue, NULL, NULL, NULL, NULL
2, test.Subject, chairs, 1, NULL, NULL, NULL
3, test.ProperNoun, James, 0, 1, NULL, NULL
4, test.Pronoun, he, 0, NULL, 1, NULL
5, test.Verb, sat, NULL, NULL, NULL, 1
And querying the list of objects back results in the proper types:
HibernateUtil.beginTransaction();
List<Word> words = HibernateUtil.getCurrentSession().createCriteria(Word.class).list();
for (Word w:words)
System.out.println(w);
HibernateUtil.commitTransaction();
Prints:
test.Word@caf6c1
test.Subject@10e35d5
test.ProperNoun@18e8541
test.Pronoun@1ce85c4
test.Verb@17aece8
See section 10.1.1 of http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/inheritance.html (it is essentially the same as the old docs though), although unfortunately it does not clearly indicate that nested subclasses are allowed -- but it is an easy thing to experiment with.
I agree that the Hibernate documentation is very poorly organized. Everything is there but I find that something about the way it is organized makes it very difficult for me to find complete information; mostly due to over use of cross-references, and a use-case oriented layout that doesn't always cover all the bases. However, the information is there.