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.
03.
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
04.
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
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
>
Now create the hibernate configuration file.
01.
"1.0"
encoding=
"UTF-8"
?>
02.
03.
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
05.
06.
07.
"hibernate.connection.driver_class"
> org.hsqldb.jdbcDriver
08.
"hibernate.connection.url"
> jdbc:hsqldb:hsql:
//localhost
09.
"hibernate.connection.username"
>sa
10.
"connection.password"
>
11.
"connection.pool_size"
>
1
12.
"hibernate.dialect"
> org.hibernate.dialect.HSQLDialect
13.
"show_sql"
>
true
14.
"hbm2ddl.auto"
>create-drop
15.
"com/vaannila/student/Student.hbm.xml"
/>
16.
17.
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.
01.
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.
}
01.
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
Address
implements
java.io.Serializable {
09.
10.
private
String street;
11.
private
String city;
12.
private
String state;
13.
private
String zipcode;
14.
15.
public
Address() {
16.
}
17.
18.
public
Address(String street, String city, String state, String zipcode) {
19.
this
.street = street;
20.
this
.city = city;
21.
this
.state = state;
22.
this
.zipcode = zipcode;
23.
}
24.
25.
public
String getStreet() {
26.
return
this
.street;
27.
}
28.
29.
public
void
setStreet(String street) {
30.
this
.street = street;
31.
}
32.
33.
public
String getCity() {
34.
return
this
.city;
35.
}
36.
37.
public
void
setCity(String city) {
38.
this
.city = city;
39.
}
40.
41.
public
String getState() {
42.
return
this
.state;
43.
}
44.
45.
public
void
setState(String state) {
46.
this
.state = state;
47.
}
48.
49.
public
String getZipcode() {
50.
return
this
.zipcode;
51.
}
52.
53.
public
void
setZipcode(String zipcode) {
54.
this
.zipcode = zipcode;
55.
}
56.
57.
}
01.
package
com.vaannila.student;
02.
03.
import
org.hibernate.HibernateException;
04.
import
org.hibernate.Session;
05.
import
org.hibernate.Transaction;
06.
07.
import
com.vaannila.util.HibernateUtil;
08.
09.
public
class
Main {
10.
11.
public
static
void
main(String[] args) {
12.
Session session = HibernateUtil.getSessionFactory().openSession();
13.
Transaction transaction =
null
;
14.
try
{
15.
transaction = session.beginTransaction();
16.
Address address =
new
Address(
"OMR Road"
,
"Chennai"
,
"TN"
,
"600097"
);
17.
Student student =
new
Student(
"Eswar"
, address);
18.
session.save(student);
19.
transaction.commit();
20.
}
catch
(HibernateException e) {
21.
transaction.rollback();
22.
e.printStackTrace();
23.
}
finally
{
24.
session.close();
25.
}
26.
27.
}
28.
29.
}
Each student has one address and the values are stored in the same STUDENT table.
The folder structure of the example is shown below.