In this example you will learn how to map many-to-many relationship using Hibernate. Consider the following relationship between Student and Course entity.
According to the relationship a student can enroll in any number of courses and the course can have any number of students.
To create this relationship you need to have a STUDENT, COURSE and STUDENT_COURSE table. The relational model is shown below.
To create the STUDENT and COURSE tables you need to create the following hibernate mapping files.
Student.hbm.xml is used to create the STUDENT and STUDENT_COURSE 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"
length
=
"100"
not-null
=
"true"
column
=
"STUDENT_NAME"
/>
12.
<
set
name
=
"courses"
table
=
"STUDENT_COURSE"
cascade
=
"all"
>
13.
<
key
column
=
"STUDENT_ID"
/>
14.
<
many-to-many
column
=
"COURSE_ID"
class
=
"com.vaannila.student.Course"
/>
15.
set
>
16.
class
>
17.
hibernate-mapping
>
Course.hbm.xml is used to create the COURSE 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.Course"
table
=
"COURSE"
>
07.
<
meta
attribute
=
"class-description"
>
08.
This class contains course details.
09.
meta
>
10.
<
id
name
=
"courseId"
type
=
"long"
column
=
"COURSE_ID"
>
11.
<
generator
class
=
"native"
/>
12.
id
>
13.
<
property
name
=
"courseName"
type
=
"string"
column
=
"COURSE_NAME"
/>
14.
class
>
15.
hibernate-mapping
>
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.
"com/vaannila/student/Course.hbm.xml"
/>
17.
18.
The following classes will be generated.
01.
package
com.vaannila.student;
02.
03.
// Generated May 30, 2009 6:48:40 AM by Hibernate Tools 3.2.4.GA
04.
05.
import
java.util.HashSet;
06.
import
java.util.Set;
07.
08.
/**
09.
* This class contains the student details.
10.
*/
11.
public
class
Student
implements
java.io.Serializable {
12.
13.
private
long
studentId;
14.
private
String studentName;
15.
private
Set courses =
new
HashSet(
0
);
16.
17.
public
Student() {
18.
}
19.
20.
public
Student(String studentName) {
21.
this
.studentName = studentName;
22.
}
23.
24.
public
Student(String studentName, Set courses) {
25.
this
.studentName = studentName;
26.
this
.courses = courses;
27.
}
28.
29.
public
long
getStudentId() {
30.
return
this
.studentId;
31.
}
32.
33.
public
void
setStudentId(
long
studentId) {
34.
this
.studentId = studentId;
35.
}
36.
37.
public
String getStudentName() {
38.
return
this
.studentName;
39.
}
40.
41.
public
void
setStudentName(String studentName) {
42.
this
.studentName = studentName;
43.
}
44.
45.
public
Set getCourses() {
46.
return
this
.courses;
47.
}
48.
49.
public
void
setCourses(Set courses) {
50.
this
.courses = courses;
51.
}
52.
53.
}
01.
package
com.vaannila.student;
02.
03.
// Generated May 30, 2009 6:48:40 AM by Hibernate Tools 3.2.4.GA
04.
05.
/**
06.
* This class contains the course details.
07.
*
08.
*/
09.
public
class
Course
implements
java.io.Serializable {
10.
11.
private
long
courseId;
12.
private
String courseName;
13.
14.
public
Course() {
15.
}
16.
17.
public
Course(String courseName) {
18.
this
.courseName = courseName;
19.
}
20.
21.
public
long
getCourseId() {
22.
return
this
.courseId;
23.
}
24.
25.
public
void
setCourseId(
long
courseId) {
26.
this
.courseId = courseId;
27.
}
28.
29.
public
String getCourseName() {
30.
return
this
.courseName;
31.
}
32.
33.
public
void
setCourseName(String courseName) {
34.
this
.courseName = courseName;
35.
}
36.
37.
}
01.
package
com.vaannila.student;
02.
03.
import
java.util.HashSet;
04.
import
java.util.Set;
05.
06.
import
org.hibernate.HibernateException;
07.
import
org.hibernate.Session;
08.
import
org.hibernate.Transaction;
09.
10.
import
com.vaannila.util.HibernateUtil;
11.
12.
public
class
Main {
13.
14.
public
static
void
main(String[] args) {
15.
16.
Session session = HibernateUtil.getSessionFactory().openSession();
17.
Transaction transaction =
null
;
18.
try
{
19.
transaction = session.beginTransaction();
20.
21.
Set courses =
new
HashSet();
22.
courses.add(
new
Course(
"Maths"
));
23.
courses.add(
new
Course(
"Computer Science"
));
24.
25.
Student student1 =
new
Student(
"Eswar"
, courses);
26.
Student student2 =
new
Student(
"Joe"
, courses);
27.
session.save(student1);
28.
session.save(student2);
29.
30.
transaction.commit();
31.
}
catch
(HibernateException e) {
32.
transaction.rollback();
33.
e.printStackTrace();
34.
}
finally
{
35.
session.close();
36.
}
37.
38.
}
39.
}
The STUDENT table has two records.
The COURSE table has two records.
The STUDENT_COURSE table has four records to link the student and courses.
Each student has enrolled in the same two courses, this illustrates the many-to-many mapping.
The folder structure of the example is shown below.