File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes One-to-One mapping  problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "One-to-One mapping  problem" Watch "One-to-One mapping  problem" New topic

One-to-One mapping problem

Gali Naresh

Joined: Jun 16, 2005
Posts: 4
hi, i am trying to create one-to-one mapping
i am getting it difficult to understand this though it looks simple.

Please find the following DB scenario:

Person(person_id(pk), person_name)
Address(address_id(unique), person_id(fk), street, city, zip)

how should my hbm and pojos should look like.

i have create mapping as below
person mapping:
<class name="com.cs.test.Person" table="Person">
<id name="personId" type="long" column="person_id">
<generator class="assigned" />
<property name="personName" column="person_name" type="string" />
<one-to-one name="address" class="com.cs.test.Address" />
Please correct the bolded tag......

address mapping:
<class name="com.cs.test.Address" table="Address">
<id name="address_id" type="long" column="address_id">
<generator class="assigned" />
<property name="street" column="street" type="string" />
<property name="city" column="street" type="string" />
<property name="zip" column="street" type="string" />

my pojos:
public class Person {
private long personId;
private String personName;
private Address address;
// getters and setters for all properties
public class Address {
private long address_id;
private String street;
private String city;
private String zip;
// getters and setters for all properties

and my java code to save the person:

Person p = new Person();
Address address = new Address();
address.setStreet("street 1");
address.setCity("city 1");

Please correct me in undersanding one-to-one mapping.

Kuladip Yadav
Ranch Hand

Joined: Jul 30, 2008
Posts: 162

Originally posted by Gali Naresh:

Person(person_id(pk), person_name)
Address(address_id(unique), person_id(fk), street, city, zip)

From your config file & Pojos

Your Schma may be like this :
Person(person_id(pk), person_name, address_id(fk))
Address(address_id(unique), street, city, zip)


For more understanding Check this one
I agree. Here's the link:
subject: One-to-One mapping problem
It's not a secret anymore!