-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Unable to map an 1-M association to an ideal-database schema
PostPosted: Thu Dec 08, 2005 3:43 am 
Newbie

Joined: Wed Oct 19, 2005 10:06 am
Posts: 6
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]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.