Monday, February 20, 2012

Creating Foreign Key RelationShip in Hibernate

I am just starting exploring Hibernate in detail. I always wanted to see how just writing pojo classes make DB operations fun.

For my recent exploration i wanted to create Foreign Key relation between two tables. Here is a simple approach i tired:

Create two tables : Person and Person_Email.
As one person can have multiple emails, so these two tables have a one-to-many relation which means one entity of Person can have amy entities of Person_Email dependent on it.

Here is the schema of two tables ( i am using MYSQL DB as its a freeware).


PERSON_ID               int(10),
AGE                            smallint(6),
FIRSTNAME              varchar(10),
LASTNAME               varchar(10)


PERSON_ID               int(10),

EMAIL_ADDRESS    varchar(255) 

Here is the corresponding POJO class. Note we dont have any POJO class for Person_Email table, as i will be taken care in Person.hbm.xml which we will see later

public class Person {

private long id;
private int age;
private String firstName;
private String lastName;
private Set emails;


public Set getEmails() {
    return emails;

public void setEmails(Set emails) {
   this.emails = emails;


Here I have ommitted the getters and setters of fields of Person tables. One thing to notice is that we have defined a Set of emails. In normal java concept, it means that Person class will have a Set object which holds all the email addresses associated with particular peron entity.

Below is the mapping class of Person Table:

<hibernate-mapping package="com.hibernate.tutorial.bean">

        <class name="Person" table="PERSON">
                <id column="PERSON_ID" name="id"></id>
                       <generator class="increment">
                <property name="firstName">
                <property name="lastName">
                <set name="emails" table="PERSON_EMAIL"></set>
                        <key column="PERSON_ID"></key>
                        <element column="email_address" type="string"></element>

In mapping file we will define the relationship between two tables.

<set name="emails" table="PERSON_EMAIL"></set>

It shows that instance property emails defined in Person class is related to table Person_Email.

<key column="PERSON_ID"></key> 

Key property shows the coolumn of Person class which is used to create the relation between two entities. We can see that PERSON_ID is a foreign key in Person_Email table.

<element column="email_address" type="string"></element>

Element property shows the rest of the columns present in Person_email table and the type of those columns.

Below is the test code to see how the values will be inserted in both the tables.

Person person = (Person)session.load(Person.class, 1001);

Here i have loaded the existing person entity from database and then adding the email address to the Set. will save the person entity and because person_email table is mapped to person table (as defined in mapping file), so new row will be inserted in Person_Email table

No comments:

Post a Comment