I had something like this to do on our product database with things like features, specifications, addons, and warranties, and a single Category.
In my Product class, I have lists of features, specifications, addons, warranties, and a single Category instance.
I explicitly define even the more primitive types, but I hope it's readable and useful.
I did the following with the addons:
In the Product.hbm.xml mapping I included this bit:
<bag name="addons" table="addon">
<key column="product_id" />
<many-to-many column="addon_product_id" class="com.mycompany.business.product.Product" />
</bag>
For its own Addon.hbm.xml mapping I do this:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<class name="com.mycompany.business.product.Addon" table="addon">
<composite-id unsaved-value="none">
<key-many-to-one class="com.mycompany.business.product.Product" name="productId" column="product_id"/>
<key-many-to-one class="com.mycompany.business.product.Product" name="addonProductId" column="addon_product_id"/>
</composite-id>
</class>
</hibernate-mapping>
If it's only a single value, you might try something like I used for a product's category:
In the Product.hbm.xml mapping:
<many-to-one name="category" column="category_id" class="com.mycompany.business.category.Category" />
In the Category.hbm.xml mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<class name="com.mycompany.business.category.Category" table="category">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" column="name" type="java.lang.String" />
<property name="code" column="code" type="java.lang.String" />
</class>
</hibernate-mapping>
|