-->
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.  [ 5 posts ] 
Author Message
 Post subject: One-to-One Mapping Issue
PostPosted: Tue Nov 04, 2003 4:54 pm 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
Gurus !
I have a couple of tables which have a one to one relational mapping. When I assign an Address object to a Person and save Person, the respective records get created in the database tables but the Person_Id in the Address table is null. Can anybody please help to what I'm supposed to do if I need the Person_id in the Address table ?

Thanks a Million !



Code:

Person object has setters and getters for Address and the Person mapping looks like this:

<one-to-one name="address" class="test.Address"
   cascade="save-update"
   outer-join="false"
   constrained="true"/>

CREATE TABLE PERSON (
       PERSON_ID           INTEGER NOT NULL,
       FIRST_NAME           VARCHAR2(30) NOT NULL,
       LAST_NAME            VARCHAR2(20) NOT NULL,
       SEX                  VARCHAR2(7) NOT NULL,
       BIRTH_DATE           DATE NOT NULL
);

ALTER TABLE PERSON
       ADD  ( PRIMARY KEY (PERSON_ID) ) ;


CREATE TABLE ADDRESS (
       ADDRESS_ID           INTEGER NOT NULL,
       ADDRESS1             VARCHAR2(40) NOT NULL,
       CITY                 VARCHAR2(40) NULL,
       STATE                VARCHAR2(30) NULL,
       ZIP                  INTEGER NULL,
       PERSON_ID           INTEGER NULL
);

ALTER TABLE ADDRESS
       ADD  ( PRIMARY KEY (ADDRESS_ID) ) ;

ALTER TABLE ADDRESS
       ADD  ( FOREIGN KEY (PERSON_ID)
                             REFERENCES PERSON
                             ON DELETE SET NULL ) ;


Top
 Profile  
 
 Post subject: mappings?
PostPosted: Tue Nov 04, 2003 5:13 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Can you post your mappings?

Without seeing them I would guess wildly that you are missing the mapping for the foreign key or missing a relationship et. al.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 6:12 pm 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
Hi DavidNDuffy !
Thanks for looking into the issue. I have the mapping files for the Person and Address objects down below. If it helps , since this is a one to one relationship, if I make the Person_Id of the address object the Primary key and get rid of the sequence from the mapping, it should be good enough. I'm not sure about it though.

Code:
Person Mapping--------------------------------------
<hibernate-mapping>
<class name="test.Person" schema="Test" table="PERSON">

<id column="PERSON_ID" name="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">PERSON_SEQ</param>
</generator>
</id>

<property column="FIRST_NAME" length="30" name="firstName" not-null="true" type="string"/>
<property column="LAST_NAME" length="20" name="lastName" not-null="true" type="string"/>
<property column="SEX" length="7" name="sex" not-null="true" type="string"/>
<property column="BIRTH_DATE" length="7" name="birthDate" not-null="true" type="timestamp"/>
</class>
</hibernate-mapping>


Address Mapping----------------------------------------

<hibernate-mapping>
<class name="test.Address" schema="TEST" table="ADDRESS">

<id column="ADDRESS_ID" name="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">ADDRESS_SEQ</param>
</generator>
</id>

<property column="ADDRESS1" length="40" name="address1" not-null="true" type="string"/>
<property column="CITY" length="40" name="city" type="string"/>
<property column="STATE" length="30" name="state" type="string"/>
<property column="ZIP" length="22" name="zip" type="java.lang.Integer"/>
<property column="PATIENT_ID" length="22" name="patientId" type="java.lang.Integer"/>

</class>
</hibernate-mapping>



Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 6:30 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Quote:
if I make the Person_Id of the address object the Primary key and get rid of the sequence from the mapping, it should be good enough


You could do that, in fact if they are truly one-to-one you should do that. But I don't actually see any relationship mapped between your two classes. Where or how are you defining this one-to-one relationship?

I think you need to have a reference to the other class on at least one of them.

Code:
public class Person {
    private Address address;
    // ...
}


in person mapping:
Code:
<one-to-one name="address" class="test.Address" cascade="all" constrained="true"/>


Please carefully read section 4.1.11 one-to-one of the documentation.

If more than one person can have the same address you would need a many-to-one or if a person can have more than one address you would need a one-to-many but all of them require some kind of reference otherwise you are probably treating instances of your classes as values and not as entities with relationships.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 1:44 am 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
If you can see my first posting the Person object has the Address mapped to it. I just wasn't sure if that is the case. Thanks for the follow up.


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