-->
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: Many-To-Many W/ a Composite Key
PostPosted: Sun May 11, 2008 4:08 pm 
Beginner
Beginner

Joined: Thu Dec 08, 2005 12:18 pm
Posts: 21
Location: Birmingham, Alabama
I am hoping someone can guide me through this issue. I have solved this problem earlier using JPA, but I now must use Hibernate only.

This is the annotation I used in JPA.

Code:
@ManyToMany(cascade={CascadeType.ALL})
      @JoinTable(name="DOCUMENT_COLLECTION",
                  joinColumns=@JoinColumn(name="DC_DOCUMENT_ID", referencedColumnName="DOCUMENT_ID"),
                  inverseJoinColumns={@JoinColumn(name="DC_COLLECTION_SITE", referencedColumnName="SITE_ID"),
                                      @JoinColumn(name="DC_COLLECTION_NAME", referencedColumnName="NAME"),
                                      @JoinColumn(name="DC_COLLECTION_GROUPDATE", referencedColumnName="GROUP_DATE")}



The goal is a document may belong to many groups. A group may have many documents. How I handled this using JPA was create a Document_Collection table. I have done the same in Hibernate by creating a table called document_collection_groupXref.

I've tried to follow the "Java Persistence with Hibernate" book in creating my solution. Here are the hibernate files


jtdiDocument.hbm.xml

Code:

<hibernate-mapping default-cascade="all">
    <class name="jtdi.metadata.orm.JtdiDocument" table="jtdi_document" catalog="jtdi_metadata_db">
        <id name="jtdiDocumentId" type="java.lang.Integer">
            <column name="jtdi_document_id" />
            <generator class="native" />
        </id>
....
       
         <set name="documentCollectionGroup" inverse="true">
            <key><column name="dc_document_id" not-null="true" /></key>
            <one-to-many class="jtdi.metadata.orm.DocumentCollectionGroupXref" />
        </set>
       
    </class>
</hibernate-mapping>


CollectionGroup.hbm.xml

Code:
<hibernate-mapping default-cascade="all">
    <class name="jtdi.metadata.orm.CollectionGroup" table="collection_group" catalog="jtdi_metadata_db">
   
        <composite-id name="id" class="jtdi.metadata.orm.CollectionGroupId">
            <key-property name="siteId" type="java.lang.String">
                <column name="site_id" length="50" />
            </key-property>
            <key-property name="groupName" type="java.lang.String">
                <column name="group_name" length="50" />
            </key-property>
            <key-property name="groupDate" type="java.lang.String">
                <column name="group_date" length="50" />
            </key-property>
        </composite-id>
       
....
       

        <set name="documentCollectionGroup" table="document_collection_group_xref">
            <key><column name="id" not-null="true" /> </key>
            <composite-element class="jtdi.metadata.orm.DocumentCollectionGroupXref">
                <parent name="CollectionGroup"/>
                <many-to-one name="JtdiDocument"
                             column="jtdi_document_id"
                             not-null="true"
                             class="jtdi.metadata.orm.JtdiDocument" />
            </composite-element>
        </set>
    </class>
</hibernate-mapping>



Lastly, DocumentCollectionGroupXref.hbm.xml

Code:

<hibernate-mapping default-cascade="all">
    <class name="jtdi.metadata.orm.DocumentCollectionGroupXref"
           table="document_collection_group_xref"
           catalog="jtdi_metadata_db"
           mutable="false">
       
       
        <composite-id name="id" class="jtdi.metadata.orm.DocumentCollectionGroupXrefId">
            <key-property name="dcDocumentId" type="java.lang.Integer">
                <column name="dc_document_id" />
            </key-property>
            <key-property name="dcCollectionSite" type="java.lang.String">
                <column name="dc_collection_site" length="50" />
            </key-property>
            <key-property name="dcCollectionGroupName" type="java.lang.String">
                <column name="dc_collection_group_name" length="50" />
            </key-property>
            <key-property name="dcCollectionGroupDate" type="java.lang.String">
                <column name="dc_collection_group_date" length="50" />
            </key-property>
        </composite-id>
       
        <many-to-one name="jtdi_document"
                     column="jtdi_document_id"
                     not-null="true"
                     insert="false"
                     update="false"/>
                     
        <many-to-one name="collection_group"
                     column="id"
                     not-null="true"
                     insert="false"
                     update="false"/>
                     
                     
    </class>
</hibernate-mapping>


I've thoroughly confused myself on how to proceed. In JPA I never had to touch the xref table. All I had to do was add the collection group to the Document class and add the document to the CollectecionGroup Class and JPA would do all the mapping to the cross-reference table.

What the Hibernate book is tell me to maintain a Set of DocumentCollectionGroupXref classes in the Document class and in the CollectionGroup Class. However, this approach assumes a Group already exist.

Any counsel would be greatly appreciated.


Russ


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.