-->
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.  [ 3 posts ] 
Author Message
 Post subject: Questions @ associations
PostPosted: Fri Jul 07, 2006 8:34 am 
Regular
Regular

Joined: Mon May 08, 2006 6:00 am
Posts: 53
Location: India
Hi I have 2 questions @ associations

1) I have 2 tables, TableOne and TableMany

TableOne has a composite key as shown below

<composite-id>

<key-property name="key1" column="PKCOLUMN1" />
<key-property name="key2" column="PKCOLUMN2" />

</composite-id>


TableMany has also a compositeKey as shown below

<composite-id>

<key-property name="key3" column="PKCOLUMN3" />
<key-property name="key1" column="PKCOLUMN1" />

</composite-id>

<property name="key2" column="PKCOLUMN2" />


Above tables are related by relation one to many.........now I have to define a relation between these 2 tables using key2 only.

In short we are trying to define a relation on part of the composite key........


2) Second question is, when we define 2 many-to-one association in one .hbm.xml file(as shown below) using same column , it

throws a error saying using a repeated column use insert="false" update="false"

<many-to-one name="association1" class="class1" insert="false" update="false" >
<column name="key1" />
<column name="key2" />
</many-to-one>


<many-to-one name="association2" class="class2" insert="false" update="false" >
<column name="key1" />
<column name="key3" />
</many-to-one>



hope i was clear enough to stae problem.....glad if any one can throw light on above 2 issues


Sudhir


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 4:48 pm 
Newbie

Joined: Fri Jul 07, 2006 10:06 am
Posts: 5
Hi,

I try to answer your Q1.

You can not use a partial key to define an association unless that column maintains uniqueness property.

So may be you should try to make a unique index on key2. If it is not unique, you can not make association anyway.

Paul


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 7:50 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 2:43 pm
Posts: 29
Location: Silicon Valley
Try using <formula> instead of <column> to define the associations:

Code:
<hibernate-mapping package="foo">
   <class name="TableOne">
      <composite-id>
         <key-property name="key1" column="PKCOLUMN1" type="string" />
         <key-property name="key2" column="PKCOLUMN2" type="string" />
      </composite-id>
   </class>
   
   <class name="TableOtherOne">
      <composite-id>
         <key-property name="key3" column="PKCOLUMN3" type="string" />
         <key-property name="key2" column="PKCOLUMN2" type="string" />
      </composite-id>
   </class>
   
   <class name="TableMany">
      <composite-id>
         <key-property name="key3" column="PKCOLUMN3" type="string" />
         <key-property name="key1" column="PKCOLUMN1" type="string" />
      </composite-id>

      <property name="key2" column="PKCOLUMN2" />

      <many-to-one name="association1" class="TableOne">
         <formula>key1</formula>
         <formula>key2</formula>
      </many-to-one>

      <many-to-one name="association2" class="TableOtherOne">
         <formula>key3</formula>
         <formula>key2</formula>
      </many-to-one>
   </class>
</hibernate-mapping>


generates DDL:
Code:
    create table TableMany (
        PKCOLUMN3 varchar(255) not null,
        PKCOLUMN1 varchar(255) not null,
        PKCOLUMN2 varchar(255),
        primary key (PKCOLUMN3, PKCOLUMN1)
    );

    create table TableOne (
        PKCOLUMN1 varchar(255) not null,
        PKCOLUMN2 varchar(255) not null,
        primary key (PKCOLUMN1, PKCOLUMN2)
    );

    create table TableOtherOne (
        PKCOLUMN3 varchar(255) not null,
        PKCOLUMN2 varchar(255) not null,
        primary key (PKCOLUMN3, PKCOLUMN2)
    );


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

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.