-->
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.  [ 4 posts ] 
Author Message
 Post subject: small problem with mapping
PostPosted: Thu Mar 09, 2006 8:15 am 
Newbie

Joined: Thu Mar 09, 2006 7:58 am
Posts: 2
Hello, friends!
I can not uderstand how to use mapping for discribing foreign key:
I want to create foregn key as Table1.Id --> Table2.Id.
But Table1 has 1 primary key and Table 2 has 2 primary key.


Table1
----------------
Name_Id (PK)
----------------
Id (FK)



Table2
----------------
Id(PK)
Parametr(PK)
---------------
Col1
Col2
Col3
..
---------------

Could you help me, please!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 5:40 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Code:
<class name="Class1" table="Table1" ...>
  ...
  <set name="Class2s" inverse="true">
    <key column="ColumnInTable2ReferringToTable1" property-ref="PropertyInClass1ReferringToClass2"/>
    <one-to-many class="Class2"/>
  </set>
...
</class>

<class name="Class2" table="Table2" ...>
  ...
  <many-to-one name="Parent" class="Class1" formula="ColumnInTable2ReferringToTable1"/>
  ...
</class>
I may have got the columns in the <key> tag the wrong way around, I'm working off memory here. If this doesn't work, try flipping them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 4:41 am 
Newbie

Joined: Thu Mar 09, 2006 7:58 am
Posts: 2
Hello!
May be You do not understand.
I want to malke the mapping for follow structute:

Table1 Class1
---------------
- Name(PK) -
---------------
- D_ID (FK) -
---------------

Table2 CLass2
---------------
- ID(PK) -<-------(D_ID from table1)
- LID(PK) -
---------------
- Param1 -
- Param2 -
- Param3 -
---------------

I Used code below, but this structure does not allow save two record as (ID=1, LID=1, Param1=0, Param2=0, Param3=0) and (ID=1, LID=2, Param1=0, Param2=0, Param3=0).
It only REPLACE parameters. I thik it happens because column "LID" does not mark at mapping as primary key.


<hibernate-mapping package="com...." default-cascade="save-update">

<class name="Class1" table="TABLE1" schema="MAIN" >

<id name="Name" column="Name">
<generator class="assigned" />
</id>

<many-to-one name="D_ID" column="D_ID" class="Class2" outer-join="true" > </many-to-one>

</class>

<class name="Class2" table="TABLE2" schema="MAIN">


<id name="id" column="ID"> <generator class="identity" /> </id>

<property name="lID" column="LID"></property>

<property name="param1" column="Param1"></property>

<property name="param1" column="Param1"></property>

<property name="param1" column="Param1"></property>

</class>

</hibernate-mapping>


When I change mapping for Class2 on code below I got the mistake. What is problem?

<class name="Class2" table="TABLE2" schema="MAIN">

<composite-id>
<key-property name="dsc_id" column="DSC_ID"/>
<key-property name="lang" column="LANG"/>
</composite-id>

<property name="param1" column="Param1"></property>

<property name="param1" column="Param1"></property>

<property name="param1" column="Param1"></property>
</class>


Could you help me, please!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 5:04 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
My previous post would handle that situation: the important point is the property-ref attribute.

If you don't want to use a collection mapping (like <set>), then your mapping is close. The main problem is that you used a key-property instead of a key-many-to-one: if you use a key-property in Class2 to refer to the PK of Class1, then hibernate has no idea that they're actually the same thing. They're separate values. Change
Code:
<composite-id>
<key-property name="dsc_id" column="DSC_ID"/>
<key-property name="lang" column="LANG"/>
</composite-id>
to
Code:
<composite-id>
  <key-many-to-one name="dsc_id" column="DSC_ID" class="Class1"/>
  <key-property name="lang" column="LANG"/>
</composite-id>
Read up on that attribute, it's in section 5.1.5, "composite-id", of the 3.1 ref docs (though admittedly, the documentation on key-many-to-one is quite poor).

When posting code, please use code tags. The code you posted is very hard to read.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.