Search

Hibernate Component Mapping Example Part - 1

In this example you will learn how to map components using Hibernate. Consider the following relationship between Student and Address entity.





According to the relationship each student should have a unique address.
Since the Student and Address entities are strongly related (composition relation), it is better to store them in a single table. The relational model is shown below.
Student.hbm.xml is used to create the STUDENT table.

01.<?xml version="1.0"?>
02.<!DOCTYPE hibernate-mapping PUBLIC
03."-//Hibernate/Hibernate Mapping DTD 3.0//EN"
05.<hibernate-mapping>
06.<class name="com.vaannila.student.Student" table="STUDENT">
07.<meta attribute="class-description">This class contains student details.</meta>
08.<id name="studentId" type="long" column="STUDENT_ID">
09.<generator class="native" />
10.</id>
11.<property name="studentName" type="string" not-null="true"
12.column="STUDENT_NAME" />
13.<component name="studentAddress"class="com.vaannila.student.Address">
14.<property name="street" column="ADDRESS_STREET" type="string"
15.length="250" />
16.<property name="city" column="ADDRESS_CITY" type="string"
17.length="50" />
18.<property name="state" column="ADDRESS_STATE" type="string"
19.length="50" />
20.<property name="zipcode" column="ADDRESS_ZIPCODE" type="string"
21.length="10" />
22.</component>
23.</class>
24.</hibernate-mapping>
The component element is used to map all the Address entity fields to the STUDENT table. In Hibernate terms the Address entity is called the component and it cannot have its own primary key, it uses the primary key of the enclosing Student entity.

Now create the hibernate configuration file.

01.<?xml version="1.0" encoding="UTF-8"?>
02.<!DOCTYPE hibernate-configuration PUBLIC
03."-//Hibernate/Hibernate Configuration DTD 3.0//EN"
05.<hibernate-configuration>
06.<session-factory>
07.<property name="hibernate.connection.driver_class"> org.hsqldb.jdbcDriver</property>
08.<property name="hibernate.connection.url"> jdbc:hsqldb:hsql://localhost<;/property>
09.<property name="hibernate.connection.username">sa</property>
10.<property name="connection.password"></property>
11.<property name="connection.pool_size">1</property>
12.<property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</property>
13.<property name="show_sql">true</property>
14.<property name="hbm2ddl.auto">create-drop</property>
15.<mapping resource="com/vaannila/student/Student.hbm.xml"/>
16.</session-factory>
17.</hibernate-configuration>
After creating the configuration file, generate java class files using Hibernate Tools.(To generate code using Hibernate Tools refer this example )
The following classes will be generated. You will still have a Student and Address class seperately but they will be mapped to only one table.

package com.vaannila.student;
02. 
03.// Generated Sep 3, 2009 6:57:20 PM by Hibernate Tools 3.2.4.GA
04. 
05./**
06.* This class contains student details.
07.*/
08.public class Student implements java.io.Serializable {
09. 
10.private long studentId;
11.private String studentName;
12.private Address studentAddress;
13. 
14.public Student() {
15.}
16. 
17.public Student(String studentName) {
18.this.studentName = studentName;
19.}
20. 
21.public Student(String studentName, Address studentAddress) {
22.this.studentName = studentName;
23.this.studentAddress = studentAddress;
24.}
25. 
26.public long getStudentId() {
27.return this.studentId;
28.}
29. 
30.public void setStudentId(long studentId) {
31.this.studentId = studentId;
32.}
33. 
34.public String getStudentName() {
35.return this.studentName;
36.}
37. 
38.public void setStudentName(String studentName) {
39.this.studentName = studentName;
40.}
41. 
42.public Address getStudentAddress() {
43.return this.studentAddress;
44.}
45. 
46.public void setStudentAddress(Address studentAddress) {
47.this.studentAddress = studentAddress;
48.}
49. 
50.}

No comments:

Post a Comment