-->
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 mapping best choice
PostPosted: Thu Oct 16, 2003 3:30 pm 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
I have the object Laboratory and ProductionCenter.

The laboratory have 2 CoordinateModel and the production center 1.

Code:
class Laboratory {
  CoordinateModel shippingAddress;
  CoordinateModel invoicingAddress;
}

class ProductionCenter {
CoordinateModel address;
}

class CoordinateModel {
String address;
String city;
}

I want to be able to share the CoordinateModel between Laboratory and ProductionCenter. I don't want to have 2 addresses on the ProductionCenter, so I don't want to use inheritance.

How can I configure my mapping for the laboratory and address to be able do what I want?

I have tried this:
Code:
<hibernate-mapping>
<hibernate-mapping>
  <class name="dti.xmscore.model.cCoordinateModel" table="contact">
    <id name="id" column="contact_id" type="long" unsaved-value="0">
      <generator class="hilo"/>
    </id>

    <property name="aStreet" column="street" type="string" length="30"/>
    <property name="aCity" column="city" type="string" length="30"/>
  </class>
</hibernate-mapping>


And the mapping for the cLaboratory:
Code:
<hibernate-mapping>
  <class name="dti.xmscore.model.cLaboratory" table="lab">
    <id name="aNewLabID" column="lab_id" type="string" length="20" unsaved-value="null">
      <generator class="assigned"/>
    </id>
    <property name="aManagerName" column="manager_name" type="string" length="30"/>
    <one-to-one name="aShippingContact" class="dti.xmscore.model.cCoordinateModel" cascade="all" constrained="true"/>
  </class>
</hibernate-mapping>


When I generate my DB, no column is added to the cLaboratory to contains the CoordinateModel. When I run the code, the contact I added to the DB, but for sure, no contact_id is added to the laboratory.

Here is my code to store the Laboratory:
Code:
cLaboratory lab = new cLaboratory("lab200000240");
cCoordinateModel lContact = cCoordinateModel("1000 street", "city");
lab.setaShippingContact(lContact);


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 10:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
One-to-one (atleast the way you have mapped it) uses the primary keys as the join so they need to be the same type and value. Hibernate 2.1 introduces foreign key for a one-to-one mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 7:03 am 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
Can you explain a little bit more. I don't understand.

Did there's a way to acheive what I want.

Can I put something else than on-to-one?

Thanks.

david wrote:
One-to-one (atleast the way you have mapped it) uses the primary keys as the join so they need to be the same type and value. Hibernate 2.1 introduces foreign key for a one-to-one mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 7:33 am 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
I have respond to myself.

I have put a many-to-one relation into the cLaboratory, so the cCoordinateModel key is mapped in the cLaboratory.

It work fine.

My mapping is:
Code:
<hibernate-mapping>
  <class name="dti.xmscore.model.cLaboratory" table="lab">
    <id name="aNewLabID" column="lab_id" type="string" length="20" unsaved-value="null">
      <generator class="assigned"/>
    </id>
    <property name="aManagerName" column="manager_name" type="string" length="30"/>

   <many-to-one name="aShippingContact" class="dti.xmscore.model.cCoordinateModel" column="shipping_contact" cascade="all"/>
   <many-to-one name="aInvoicingContact" class="dti.xmscore.model.cCoordinateModel" column="invoicing_contact" cascade="all"/>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 11:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Yes thats for one side. The other direction requires the one-to-one mapping with the property-ref attiribute.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 18, 2003 11:43 am 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
I just put a many-to-one in the cLaboratory mapping and it works fine. Why I should add a one-to-one mapping into the cCoordinateModel?

May be it could cause problems for retreiving?

Thanks.

david wrote:
Yes thats for one side. The other direction requires the one-to-one mapping with the property-ref attiribute.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 19, 2003 1:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
There is no requirement to have a bi-directional mapping so your fine. I was just pointing out that if your were going to have bi-directional one-to-one using foreign key then one side is man-to-one and the other will be one-to-one with property-ref.


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.