Hello,
I’m using Hibernate 3 from CVS.
I’ve read the documentation multiple times but I think I’m going to need some help getting over this initial hurdle then hopefully other things will click.
Let’s say I wanted to model something like the following (collectively called “entities”):
corporation
|
subsidiary
|
department
So just to confirm, if each of these are going to be in a separate table, then I would want to use the <joined-subclass> element between these, right?
Now, let’s say I had a single table (called “ENTITY_INFO”) where I store information about each of these entities. Additionally, in my object representation of entity_info I want a reference to the associated entity. Since each entity table has its own primary key, I need a secondary column (called “entity_type” here) in entity_info in order to complete the relationship. So here’s my first stab at mapping the entity_info bit in Hibernate… so somebody please correct me if I’m wrong or if there’s a better way…. Thanks!
Code:
<class lazy="true" name="entity_info" table="entity_info">
<id name="oid">
<generator class="native"/>
</id>
<property name=”phone”/>
<property name=”num_people”/>
<discriminator column="entity_type" type="integer"/>
<subclass name="entity_info_corporation" discriminator-value="1">
<many-to-one name="corporation" class="corporation ">
<column name="entity_id"/>
</many-to-one>
</subclass>
<subclass name="entity_info_subsidiary" discriminator-value="2">
<many-to-one name="subsidiary " class=" subsidiary ">
<column name="entity_id"/>
</many-to-one>
</subclass>
<subclass name="entity_info_department" discriminator-value="3">
<many-to-one name=" department " class=" department ">
<column name="entity_id"/>
</many-to-one>
</subclass>
</class>