aspose file tools*
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
Author

One-to-One mapping problem

Gali Naresh
Greenhorn

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" />
</id>
<property name="personName" column="person_name" type="string" />
<one-to-one name="address" class="com.cs.test.Address" />
</class>
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" />
</id>
<property name="street" column="street" type="string" />
<property name="city" column="street" type="string" />
<property name="zip" column="street" type="string" />
</class>

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();
p.setPersonId(1);
p.setPersonName("test");
Address address = new Address();
address.setAddressId(1);
address.setStreet("street 1");
address.setCity("city 1");
address.setZip("1234");
p.setAddress(address);

session.save(p);

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

Thanks,
NG
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)


Regards

For more understanding Check this one
 
wood burning stoves
 
subject: One-to-One mapping problem