April 14, 2011

Mapping Entity and Value types in Hibernate

Create a User Entity type persistent class and an Address Value type persistent class. Create the User.hbm.xml mapping file for the Entity type. Add the mapping for the value type in the User.hbm.xml.
Persist an object of type User.
Retrieve the same object and print a property from the Address value type.

1) Create the User.java persistent class of type entity in the src folder of the web application. The class will have a property of type Address which is a persistent class of value type.

package info.icontraining.hibernate;

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

2) Create the HomeAddress.java persistent class of type value in the src folder of the web application.

package info.icontraining.hibernate;

public class HomeAddress {

   private String street;
   private String city;
   private String zipCode;
   private User user;
   public String getStreet() {
       return street;
   public void setStreet(String street) {
       this.street = street;
   public String getCity() {
       return city;
   public void setCity(String city) {
       this.city = city;
   public String getZipCode() {
       return zipCode;
   public void setZipCode(String zipCode) {
       this.zipCode = zipCode;
   public User getUser() {
       return user;
   public void setUser(User user) {
       this.user = user;

3) Create the User.hbm.xml mapping file in the same folder/package as the User.java class - this file will contain the mapping information for the entity and the value types

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
   <class name="info.icontraining.hibernate.User" table="USER_TABLE"> 
      <id name="id" column="USER_ID"> 
         <generator class="native" /> 
      <property name="name" column="USER_NAME" /> 
      <component name="address" class="info.icontraining.hibernate.HomeAddress">
         <parent name="user" />
         <property name="street" column="STREET" />
         <property name="city" column="CITY" />
         <property name="zipCode" column="ZIPCODE" />

4) Modify the hibernate.cfg.xml file in the WebContent/WEB-INF folder to add the User.hbm.xml as a mapping resource,

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

5) Create a UserServlet.java class to execute the code to save a User object.

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 UserServlet 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(); 
      User user = new User();
      HomeAddress address = new HomeAddress();

      Session newSession = HibernateUtil.getSessionFactory().openSession(); 
      Transaction newTransaction = newSession.beginTransaction(); 
      List users = newSession.createQuery("from User as u").list(); 
      out.println( users.size() + " user/s found" ); 
      for ( Iterator iter = users.iterator(); iter.hasNext(); ) { 
         User user2 = iter.next(); 
         out.println( user2.getName() +": " + user2.getAddress().getCity());   


6) Configure the UserServlet.java in the web.xml file,


7) Test the code by typing the following URL in the browser,


No comments:

Post a Comment