-->
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.  [ 8 posts ] 
Author Message
 Post subject: One-One mapping - Is this right ?
PostPosted: Wed Nov 05, 2003 6:54 pm 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
I have a One-to-One mapping example working but not sure if its supposed to be working this way. Can anyone please let me know if the process I'm following is right ?

Code:

// The Persistance Code
Person person = makePerson(); // Faking
Address address = makeAddress(); // Faking
person.setAddress(address);
Integer i = (Integer)session.save(person);
address.setPerson(person);
address.setId(i);
i = (Integer)session.save(address);
// commit transaction


The classes roughly without the setters and getters

Code:
// Parent Class
class Person{
private Address address;
......
}
// Child
class Address{
private Person person;
......
}


The Mapping files. The domain objects have been fine-grained and hence the One-One relationship. Assuming a Person has only one Address, the mappings are :

Code:

// Person Mapping File

<hibernate-mapping>
<class name="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>

<one-to-one name="address" class="Address" constrained="false" outer-join="false"/>

<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"/>
</class>
</hibernate-mapping>

// Address Mapping

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

<id column="PERSON_ID" name="id" unsaved-value="null">
<generator class="assigned" />
</id>

<property column="ADDRESS1" length="40" name="address1" not-null="true" type="string"/>
<property column="ADDRESS2" length="40" name="address2" 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"/>

<one-to-one name="person" class="Person" constrained="true" outer-join="auto" />
</class>
</hibernate-mapping>



Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 7:05 pm 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
Technically, there should be only one call made to the session with Person populated with Address. Upon commit, the Person record and the Address records have to be created with the foreign key in Address populated with the current Person_Id. The way the above code works, I have to get the CURR_VAL from the Patient and then stick it as the foreign key in Address . The relationship has to be something like this, I guess:

Code:
tx = session.beginTransaction();
Person person = makePerson();
person.setAddress(makeAddress());
Integer i = (Integer)session.save(person);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 7:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/118.html#A9

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 7:54 pm 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
Hi-
I did try to set up my relationships based on the documentation but for some reason it doesn't work. The tests in the Hibernate source has an example with Parent and Child relationship. I used that code as an example and posted the source which works. Can you see if you can come up on top of your head as to what I'm doing wrong. The data model to go with the prior mappings is posted below. Thanks for following up.

Code:
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:
PostPosted: Thu Nov 06, 2003 7:13 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can't really understand what is your pb.

Code:
<class name="Address" schema="TEST" table="ADDRESS">
<id column="PERSON_ID" name="id" unsaved-value="null">
<generator class="foreign">
  <param name="property">person</param>
</generator>
</id>


Will let hibernate assign address id.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2003 10:46 am 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
It wasn't a problem. I was thinking a save to the Person would create a new record in Address as well but that seems to be not the case. The Address object has also to be saved too. Anyways, I guess I have got a feel for how the One-to-One relationships work. Thanks for your follow ups.

Code:

Person person = makePerson();
Address address = makeAddress();
person .setAddress(address);
session.save(person );
address.setPatient(person );
session.save(address); // Have to save address Object in session too
tx.commit();



Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2003 10:51 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
OK I understand now. Try
<one-to-one cascade="save-update" name="address"/>

And try (not related)
Code:
Person person = makePerson();
Address address = makeAddress();
person .setAddress(address);
address.setPatient(person );
session.save(person); // late save to optimize SQL insert
tx.commit();

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2003 11:37 am 
Newbie

Joined: Tue Nov 04, 2003 4:20 pm
Posts: 13
Emmanuel:
That does the trick. Your answer was worth 25 dukes. Thanks so much.


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