Hibernate version: Hibernate 3.0.5
Consider a scenario as shown in below figure.
Code:
0..1 0..n
P---------------------X
^ ^
| |
------ -------
| | | |
Q1 Q2 Z1 Z2
Here the hierarchy of P is mapped using Table-Per-ClassHierarchy and that of X is mapped using Table-Per-ConcreteClass. The cardinality of association is one-to-many (from P to X) and the association is bidirectional.
The ideal database schema for this association would be as shown below:
Code:
Table Name --> P Z1 Z2
--------- ----------- -----------
Columns --> ID ID ID
. . .
. . .
P_ID P_ID
But as I don’t know much hibernate-mapping-tags, I have mapped this using following hbm files:
Code:
P.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="chsncc.bi.one2many">
<class name="P" table="PTEST">
<id name="id" column="ID" type="java.lang.Long">
<generator class="increment"/>
</id>
<set name="xs" table="P_XTEST">
<key column="P_ID"/>
<many-to-any meta-type="string" id-type="long">
<meta-value value="Z1" class="Z1"/>
<meta-value value="Z2" class="Z2"/>
<column name="X_CLASSNAME"/>
<column name="X_ID"/>
</many-to-any>
</set>
</class>
</hibernate-mapping>
Z1.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="chsncc.bi.one2many">
<class name="Z1" table="Z1TEST">
<id name="id" column="ID" type="java.lang.Long">
<generator class="increment"/>
</id>
<join table="P_XTEST" inverse="true">
<key column="X_ID"/>
<many-to-one name="p" column="P_ID"/>
</join>
</class>
</hibernate-mapping>
Z2.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="chsncc.bi.one2many">
<class name="Z2" table="Z2TEST">
<id name="id" column="ID" type="java.lang.Long">
<generator class="increment"/>
</id>
<join table="P_XTEST" inverse="true">
<key column="X_ID"/>
<many-to-one name="p" column="P_ID"/>
</join>
</class>
</hibernate-mapping>
And the resultant database schema is as shown below:
Code:
Table Name --> P P_X Z1 Z2
--------- ------------- ----------- -----------
Fields --> ID P_ID ID ID
. X_ID . .
. X_CLASSNAME . .
This requires an extra table to be created.
Is there any way by which I can map it to the ideal schema?[/code]