-->
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.  [ 2 posts ] 
Author Message
 Post subject: <one-to-one> question
PostPosted: Wed Dec 13, 2006 1:48 pm 
Newbie

Joined: Wed Dec 13, 2006 1:38 pm
Posts: 1
I have a parent child relationship with the Person and the Address table. One Person can have 2 addresses namely PERMANENT address and MAILING address.

I already have a <one-to-many> relationship from Person to Address using the Person's primary key.

Sample table layout.

PERSON table

PERSON_KEY
FIRST NAME
LAST NAME
..
..

ADDRESS table
ADDRESS_KEY
PERSON_KEY
ADDRESS TYPE (Can be MAILING OR PERMANENT)
ADDRESS LINE1
CITY
STATE
..
..

Now, I need a one to one relationship from Person to Address which uses the ADDRESS TYPE field of the ADDRESS table. This way I can explicitly obtain the permanentAddress or the mailingAddress from the person.

For example
<one-to-one name='permanentAddress ....
</one-to-one>

Can anybody help me writing this relationship?

Thanks.


Top
 Profile  
 
 Post subject: Use a formula
PostPosted: Wed Dec 13, 2006 6:50 pm 
Newbie

Joined: Wed Dec 06, 2006 6:30 pm
Posts: 8
Check out: http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-onetoone

I hunted down the sample they refer to:

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

<hibernate-mapping package="org.hibernate.test.onetooneformula">

   <class name="Person">
      <id name="name"/>
      <one-to-one name="address" cascade="all" constrained="false">
         <formula>name</formula>
         <formula>'HOME'</formula>
      </one-to-one>
      <one-to-one name="mailingAddress" constrained="false">
         <formula>name</formula>
         <formula>'MAILING'</formula>
      </one-to-one>
   </class>
   
   <class name="Address" batch-size="2"
         check="addressType in ('MAILING', 'HOME', 'BUSINESS')">
      <composite-id>
         <key-many-to-one name="person"
               column="personName"/>
         <key-property name="type"
               column="addressType"/>
      </composite-id>
      <property name="street"/>
      <property name="state"/>
      <property name="zip"/>
   </class>

</hibernate-mapping>


-David


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