Title: RE: [castor-dev] one to one relationships
Oh wait, I was wrong.  I thought it worked in principle ( but failed due to db issues but i see that I'm still having issues ) :
 
I've set up my mapping file as such
 
 
<mapping>
 
    <key-generator name="SEQUENCE" alias="SEQ_MEMBER_ID">
        <param name="sequence" value="SEQ_MEMBER_ID"/>
        <param name="returning" value="true"/>
    </key-generator>
 
    <class name="clubmom.profile.dataobjects.Member" identity="id" key-generator="SEQ_MEMBER_ID">
 
        <map-to table="members"/>
        <field name="id" type="integer">
            <sql name="member_id" type="integer"/>
        </field>
        <field name="userName" type="string">
            <sql name="user_name" type="varchar"/>
        </field>
        <field name="password" type="string">
            <sql name="password" type="varchar"/>
        </field>
        <field name="zipCode" type="string">
            <sql name="zip_code" type="varchar"/>
        </field>
        <field name="firstName" type="string">
            <sql name="first_name" type="varchar"/>
        </field>
        <field name="lastName" type="string">
            <sql name="last_name" type="varchar"/>
        </field>
        <field name="email" type="string">
            <sql name="email" type="varchar"/>
        </field>
        <field name="profileComplete" type="integer">
            <sql name="profile_complete" type="integer"/>
        </field>
        <field name="points" type="integer">
            <sql name="points" type="integer"/>
        </field>
        <field name="memberNumber" type="string">
            <sql name="member_number" type="varchar"/>
        </field>
        <field name="memberStatus" type="integer">
            <sql name="member_status_id" type="integer"/>
        </field>
        <field name="marketName" type="string">
            <sql name="market_name" type="varchar"/>
        </field>
 
        <field name="address" type="clubmom.profile.dataobjects.MemberAddress">
            <sql many-key="member_id"/>
        </field>
 
    </class>
 
    <class name="clubmom.profile.dataobjects.MemberAddress" identity="id" depends="clubmom.profile.dataobjects.Member" >
        <map-to table="member_address"/>
 
        <field name="id" type="integer">
            <sql name="member_id" type="integer"/>
        </field>
 
        <field name="streetAddress" type="string">
            <sql name="street_address" type="varchar"/>
        </field>
        <field name="apartmentNo" type="string">
            <sql name="apartment_no" type="varchar"/>
        </field>
        <field name="zipCode" type="string">
            <sql name="zip_code" type="varchar"/>
        </field>
        <field name="city" type="string">
            <sql name="city" type="varchar"/>
        </field>
        <field name="state" type="string">
            <sql name="state" type="varchar"/>
        </field>
 
    </class>
 
</mapping>
and my java  code to store does this:
 
                // store the new member
                Member newMember = new Member();
                newMember.setUserName( currentForm.getUserName() );
                newMember.setPassword( currentForm.getPassword() );
                newMember.setEmail( currentForm.getEmail());
                newMember.setFirstName( currentForm.getFirstName() );
                newMember.setLastName( currentForm.getLastName() );
                newMember.setZipCode( currentForm.getZipCode() );
                newMember.setMemberStatus( 100 );
 
                MemberAddress newAddress = new MemberAddress();
                newAddress.setStreetAddress( currentForm.getStreetAddress() );
                newAddress.setApartmentNo( currentForm.getApartmentNo() );
                newAddress.setZipCode( currentForm.getZipCode() );
                newAddress.setCity( currentForm.getCity() );
                newAddress.setState( currentForm.getState() );
 
                newMember.setAddress( newAddress );
 
                db.create( newMember );
 
 
but i get a foreign key constraint violation from MEMBER_ADDRESS when i try to persist.
 
The main difference between my case and the example case is that they both have a MEMBER_ID
 
instead of a PK for the MEMBER_ADDRESS table ( it's PK is the FK )
 
-a
-----Original Message-----
From: Aadithya Deshpande [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, October 22, 2002 10:28 AM
To: [EMAIL PROTECTED]
Subject: Re: [castor-dev] one to one relationships

Thanks a lot. 
That really cleared it up for me.

That brings me to two questions:

1) Why not change the name of the "many-key" attribute to something like "relationship-key" or "rel-key" ?  It seems that a x - to - many mapping is dependent on 2 things: a relationship key and a collection type, whereas a one to one mapping like mine is a relationship key with an object type.

2) Has any thought been given to the use of proxy objects ( from JDK 1.3 on ) for dynamically loading up these dependent objects so it's only grabbed when needed?  I mean, I'm *sure* thought has been given, but what is the current thinking.

Again, thanks for the help and for a great piece of software.

-a



-----Original Message-----
From: Patrick van Kann [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, October 22, 2002 4:14 AM
To: [EMAIL PROTECTED]
Subject: Re: [castor-dev] one to one relationships


Hello,

Just a quick note -- There are two ways of achieving a one-to-one
mapping. That is why one-to-one discussions are a little confusing! The
following is a work-in-progress for a FAQ item on this topic. Imagine a
class Brochure that is to have a one-to-one relationship with
myapp.Product from the JDO examples (src/examples/jdo).

NB: In my model, the relationship between Product and Brochure is
composition -- hence the depends keyword in the mapping.

Approach 1
Treat as one-to-many but without collection.

Advantage: Requires no bi-directionality at the database level (see
Approach 2 below)
Disadvantage: Semantically confusing to use "many-key" since it is not
this type of relationship

References:      Thomas Yip
(http://castor.exolab.org/list-archive/msg20458.html)
                       Margaret Martin
(http://castor.exolab.org/list-archive/msg20437.html)

Example Mapping:
In the myapp.Product mapping we add

<!-- Product has reference to Brochure
        one brochure per product in a one-to-one mapping  -->
     <field name="brochure" type="myapp.Brochure">

       <sql many-key="prod_id"/>
         <xml name="brochure" node="element" />
   </field>

Approach 2
Add foreign key relationships in both tables of the database, and map it
as follows. This means adding a field to the prod table called broch_id!

Advantage: Semantically more satisfying at a mapping level
Disadvantage: Bi-directional foriegn key relationship required at a
database level

References:        Peter Jezenszky:
http://castor.exolab.org/list-archive/msg20409.html

Example Mapping:
<!-- Product has reference to Brochure
        one brochure per product in a one-to-one mapping  -->
     <field name="brochure" type="myapp.Brochure">
          <sql name="broch_id"/>
         <xml name="brochure" node="element" />
   </field>


The mapping for Brochure is the same in each case.

<!--  Mapping for Brochure  -->
 <class name="myapp.Brochure" identity="id" depends="myapp.Product">
   <description>Brochure definition</description>
   <map-to table="brochure" xml="brochure" />
   <field name="id" type="integer">
     <sql name="id" type="integer" />
     <xml name="id" node="attribute"/>
   </field>
 
   <!--  Brochure has reference to Product,
         one brochure may reference one product  -->
   <field name="product" type="myapp.Product" required="true">
     <sql name="prod_id" />
     <xml name="product" node="element" />
   </field>
 </class>

The table for brochure is

brochure
----------
id
prod_id


I hope this helps.

Patrick

Dominik Baranowski wrote:

> the scenario you describe with the set and getAddress methods is most
> definitely possible.
> how are you doing the mapping?

> -dom
>
>     ----- Original Message -----
>     *From:* Aadithya Deshpande <mailto:[EMAIL PROTECTED]>
>     *To:* [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
>     *Sent:* Monday, October 21, 2002 3:11 PM
>     *Subject:* [castor-dev] one to one relationships
>
>     i just tried re-reading the one-to-one discussion on the mailing
>     list that occurred a little while back and i'm still a bit confused.
>     
>     i'm trying to implement a 1-to-1 relationship of this type;
>     
>     MEMBER                MEMBER_ADDRESS
>     --------------                --------------------------------
>     ID                            ID
>     LOGIN                     STREET_ADDRESS
>     PASSWORD            CITY
>     EMAIL                      STATE
>     
>     
>     this is a one to one relationship tied together by ID ( obviously,
>     i hope ).
>     
>     does this have to be mapped as an "extends" relationship?  or can
>     i do something similar to the master/detail relationship?
>     
>     ideally, i'd like to go down the master/detail style route.
>     
>     public class Member {
>     
>     private int id;
>     private String login;
>     private String password;
>     private string email;
>     private Address memberAddress;
>     
>     ..gets and sets..
>     
>     public void setAddress( Address p_address ) { this.memberAddress =
>     p_address; }
>     public Address getAddress( ) { return this.memberAddress; }
>     
>     }
>     
>     can't seem to make this happen.  is this possible?
>
>     Aadi Deshpande
>     Director of Engineering
>     ClubMom, Inc.
>     200 Madison Ave., 6th Floor
>     New York,  NY 10016
>     tel 646.435.6562
>     fax 646.435.6600
>
>     
>

-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to