-->
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.  [ 7 posts ] 
Author Message
 Post subject: one-to-one assoc.: class with 3 and 2 keys for composite?
PostPosted: Tue Dec 09, 2003 9:05 am 
Newbie

Joined: Wed Dec 03, 2003 11:15 am
Posts: 13
Location: Stuttgart, Germany
Hi there:

Following situation:

Table ProductOrder has a PK that looks like this:
OrderID
CategoryID
ProductID


Table Product has a PK like this:
CategoryID
ProductID


I want to define a one-to-one association between them via the PKs "CategoryID" and "ProductID".
When I try to do so, I get the following error:

MappingException: invalid join columns for association: Product

The reason for this error is basicly clear: I have to tell hibernate which columns to join
with. But how can I do this?

Here my mapping files for the tables:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class name="com.iag.ProductOrder"
         table="product_order" lazy="true">

    <composite-id>
        <key-property name="OrderId" column="order_id" type="integer"/>
        <key-property name="CategoryId" column="category_id" type="integer"/>
        <key-property name="ProductId" column="product_id" type="integer"/>
    </composite-id>

    <version name="modified"
             unsaved-value="undefined"/>

    <property name="Text" column="text" type="string" not-null="false"/>

    <many-to-one name="CustomerOrder" column="order_id" not-null="false"
                 insert="false" update="false"/>

    <one-to-one name="Product" class="com.iag.Product"/>
  </class>
</hibernate-mapping>


And for the Product:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class name="com.iag.Product"
         table="product">

    <composite-id>
        <key-property name="CategoryId" column="category_id" type="integer"/>
        <key-property name="ProductId" column="product_id" type="integer"/>
    </composite-id>

    <version name="modified"
             unsaved-value="undefined"/>

    <property name="CategoryId" column="category_id" type="integer" not-null="true"
              insert="false" update="false"/>
    <property name="ProductId" column="product_id" type="integer" not-null="true"
              insert="false" update="false"/>
    <property name="Name" column="name" type="string" not-null="true"/>
    <property name="Price" column="price" type="double" not-null="false"/>
    <property name="Type" column="type" type="string" not-null="true"/>
  </class>
</hibernate-mapping>


Thx for any help.

Regards,
noips


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 11:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
one-to-one linked classes must refer the same id value. It cannot be your case since ids are structurally incompatible.

Have a look at the property-ref attribute of one-to-one in hibernate 2.1. Might help.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 3:18 am 
Newbie

Joined: Wed Dec 03, 2003 11:15 am
Posts: 13
Location: Stuttgart, Germany
epbernard wrote:
one-to-one linked classes must refer the same id value. It cannot be your case since ids are structurally incompatible.

They do refer to the same id value. The only difference is that is is an composite key in this case.
Nothing remarkable, don


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 3:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
They do refer to the same id value.


No they don't! One has three columns in the id, one has two. The ids can't possibly be the same.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 3:44 am 
Newbie

Joined: Wed Dec 03, 2003 11:15 am
Posts: 13
Location: Stuttgart, Germany
gavin wrote:
Quote:
They do refer to the same id value.


No they don't! One has three columns in the id, one has two. The ids can't possibly be the same.

Yes, you are right. Viewed from the Product


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 3:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
There are two approaches:


EITHER:

Code:
<many-to-one name="product"
        class="Product"
        insert="false"
        update="false"
        constrained="true">
        <column name="category_id"/>
        <column name="product_id"/>
</many-to-one>


OR:
Code:
<composite-id>
        <key-property name="OrderId" column="order_id" type="integer"/>
        <key-many-to-one name="product" class="Product">
                <column name="category_id"/>
                <column name="product_id"/>
        </key-many-to-one>
</composite-id>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 4:23 am 
Newbie

Joined: Wed Dec 03, 2003 11:15 am
Posts: 13
Location: Stuttgart, Germany
gavin wrote:
There are two approaches:


EITHER:

Code:
<many-to-one name="product"
        class="Product"
        insert="false"
        update="false"
        constrained="true">
        <column name="category_id"/>
        <column name="product_id"/>
</many-to-one>


Only if I remove the
Code:
constrained="true"
entry, this approach works.
Do you know why?

gavin wrote:
OR:
Code:
<composite-id>
        <key-property name="OrderId" column="order_id" type="integer"/>
        <key-many-to-one name="product" class="Product">
                <column name="category_id"/>
                <column name="product_id"/>
        </key-many-to-one>
</composite-id>


So both approaches do work, thx a lot!!

Regards,
noips


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.