-->
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: Composite-id mapping, three tables example
PostPosted: Mon Aug 01, 2005 2:38 pm 
Newbie

Joined: Wed Jul 27, 2005 9:50 am
Posts: 3
Hibernate version: 2.1.8

Mapping documents:
Code:
<hibernate-mapping>
   <class name="com.hibtest.ClassA" table="class_a" schema="abc" mutable="false">
      <composite-id>
         <key-property name="pka1" column="pk_a1" type="java.lang.Long"/>
         <key-property name="pka2" column="pk_a2" type="java.lang.String" length="2"/>
         <key-property name="pka3" column="pk_a3" type="java.lang.String"/>
      </composite-id>
      <property name="flda1" column="fld_a1" type="java.lang.String" length="7" not-null="true"/>
      <!-- Associations -->
      <many-to-one name="classB" class="com.hibtest.ClassB" insert="false" update="false">
         <column name="pk_b1"/>
      </many-to-one>
      <many-to-one name="classC" class="com.hibtest.ClassC" insert="false" update="false">
         <column name="pk_c1"/>
         <column name="pk_c2"/>
      </many-to-one>
   </class>
</hibernate-mapping>


<hibernate-mapping>
   <class name="com.hibtest.ClassB" table="class_b" schema="abc" mutable="false">
      <id column="pk_b1" name="pkb1" type="java.lang.Long">
         <generator class="native"/>
      </id>
      <property column="fld_b1" length="4" name="fldb1" not-null="true" type="java.lang.String"/>
      <!-- Associations -->
      <set name="classAObjects" lazy="true" cascade="none" inverse="true">
         <key>
            <column name="pk_a1"/>
            <column name="pk_a2"/>
         </key>
         <one-to-many class="com.hibtest.classA"/>
      </set>
   </class>
</hibernate-mapping>


<hibernate-mapping>
   <class name="com.hibtest.ClassC" table="class_c" schema="abc" mutable="false">
      <composite-id>
         <key-property name="pkc1" column="pk_c1" type="java.lang.Long"/>
         <key-property name="pkc2" column="pk_c2" type="java.lang.String" length="2"/>
      </composite-id>
      <property name="fldc1" column="fld_c1" type="java.lang.String" length="12" not-null="true"/>
      <!-- Associations -->
      <set name="classAObjects" lazy="true" cascade="none" inverse="true">
         <key>
            <column name="pk_a1"/>
            <column name="pk_a2"/>
         </key>
         <one-to-many class="com.hibtest.classA"/>
      </set>
   </class>
</hibernate-mapping>







Code :
Code:
/*
  Getters/setters removed for brevity in this example
/*

public class ClassA implements Serializable {
    // Many of ClassA to one of these
    private ClassB classB;
    private ClassC classC;
   
    // Our composite key fields
    private Long pka1;      // foreign key to ClassB.pkb1 and ClassC.pkc1
    private String pka2;    // foreign key to ClassC.pkc1
    private String pka3;
   
    // A regular persistent field
    private String flda1;
}

public class ClassB implements Serializable {   
    // One ClassB to many ClassA's
    private Set classAOjbects;
   
    // Our primary key field
    private Long pkb1;      // referenced by foreign key ClassA.pka1
   
    // A regular persistent field
    private String fldb1;
}

public class ClassC implements Serializable {
    // One ClassC to many ClassA's
    private Set classAObjects;
   
    // Our composite key fields
    private Long pkc1;      // referenced by foreign key ClassA.pka1
    private String pkc2;    // referenced by foreign key ClassA.pka2
   
    // A regular persistent field
    private String fldc1;
}



Tables:

---------
class_a
=====
pk_a1 --references class_c.pk_c1, references class_b.pk_b1
pk_a2 --references class_c.pk_c2
pk_a3
---------
fld_a1
---------


---------
class_b
=====
pk_b1 --referenced by class_a.pk_a1
---------
fld_b1
---------


---------
class_c
=====
pk_c1 --referenced by class_a.pk_a1
pk_c2 --referenced by class_a.pk_a2
---------
fld_c1
---------




The Question:
This beginner is stuck on what to put in the mapping documents to describe associations for these persistent classes (ClassA, ClassB, ClassC). In particular, what goes in the <many-to-one> and <set> tags in each mapping document? I can't change anything in the underlying schema because it's used by several other legacy apps which cannot be rewritten (and they use column positions versus names to access fields). We are only performing read operations on the data. Thanks for any assistance you can provide.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.