May 8, 2011

Hibernate Entity Mapping: One-to-one relationship with shared primary key association

Map a one-to-one entity relationship where the primary key of the parent table is shared as also the primary key of the child table as well as the foreign key referencing the primary key of the parent table.

The Person entity has a one-to-one relationship with the Address entity. In the database schema, the table mapped to Person will be the parent table and the table mapped to Address will be the child table.

1) Create the Player and Address entity persistent classes,

Person.java

package info.icontraining.hibernate;

public class Person {
 
   private Long id;
   private String name;
   private Address address;
 
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Address getAddress() {
      return address;
   }
   public void setAddress(Address address) {
      this.address = address;
   }
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
}


Address.java

package info.icontraining.hibernate;

public class Address {

   private Long id;
   private String city;
   private Person person;
 
   public String getCity() {
      return city;
   }
   public void setCity(String city) {
      this.city = city;
   }
   public Person getPerson() {
      return person;
   }
   public void setPerson(Person person) {
      this.person = person;
   }
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
}


2) Create the mapping files, Person.hbm.xml and Item.hbm.xml,

Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
   <class name="info.icontraining.hibernate.Person" table="PERSON_TABLE"> 
      <id name="id" column="PERSON_ID"> 
         <generator class="native" /> 
      </id> 
      <property name="name" column="PERSON_NAME" type="string" />
      <one-to-one name="address" class="info.icontraining.hibernate.Address" cascade="save-update" />
   </class> 
</hibernate-mapping>

Address.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
   <class name="info.icontraining.hibernate.Address" table="ADDRESS_TABLE"> 
      <id name="id" column="ADDRESS_ID"> 
         <generator class="foreign">
            <param name="property">person</param>
         </generator> 
      </id> 
      <property name="city" column="CITY" type="string" />
      <one-to-one name="person" class="info.icontraining.hibernate.Person" constrained="true" />
   </class> 
</hibernate-mapping>


3) In the hibernate.cfg.xml file, add the following configuration,

<mapping resource="info/icontraining/hibernate/Person.hbm.xml" />
<mapping resource="info/icontraining/hibernate/Address.hbm.xml" />


4) Create the client code in a servlet, as follows,

package info.icontraining.servlets;

import java.io.*; 
import java.util.*; 
import javax.servlet.*; 
import javax.servlet.http.* ; 
import org.hibernate.*; 
import info.icontraining.hibernate.*;

public class PersonAddress extends HttpServlet { 
  
   public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ 
  
      PrintWriter out = res.getWriter(); 

      Session session = HibernateUtil.getSessionFactory().openSession(); 
      Transaction tx = session.beginTransaction(); 
      Person person = new Person();
      person.setName("Person-1");
   
      Address address = new Address();
      address.setCity("Atlanta");
 
      person.setAddress(address);
      address.setPerson(person);
   
      session.save(person); 
   
      tx.commit(); 
      session.close(); 

      Session newSession = HibernateUtil.getSessionFactory().openSession(); 
      Transaction newTransaction = newSession.beginTransaction(); 
      List<Person> persons = newSession.createQuery("from Person").list(); 
      out.println( persons.size() + " persons found:" ); 
      for ( Iterator<Person> iter = persons.iterator(); iter.hasNext(); ) { 
         Person person1 = iter.next(); 
         out.println( person1.getName()); 
      } 
   
      newTransaction.commit(); 
      newSession.close(); 
   } 
}


5) Configure the servlet in the web.xml file,

<servlet>
   <servlet-name>PersonAddress</servlet-name>
   <servlet-class>info.icontraining.servlets.PersonAddress</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>PersonAddress</servlet-name>
   <url-pattern>/personAddress</url-pattern>
</servlet-mapping>


6) Enter the following URL in the browser to test,

http://localhost:8080/WebAppName/personAddress

No comments:

Post a Comment