Search

Hibernate Many-To-Many Mapping Using Annotations Part -2

The @ManyToMany annotation is used to create the many-to-many relationship between the Studentand Course entities. The @JoinTable annotation is used to create the STUDENT_COURSE link table and @JoinColumn annotation is used to refer the linking columns in both the tables.
Course class is used to create the COURSE table.

01.package com.vaannila.student;
02. 
03.import javax.persistence.Column;
04.import javax.persistence.Entity;
05.import javax.persistence.GeneratedValue;
06.import javax.persistence.Id;
07.import javax.persistence.Table;
08. 
09.@Entity
10.@Table(name="COURSE")
11.public class Course {
12. 
13.private long courseId;
14.private String courseName;
15. 
16.public Course() {
17.}
18. 
19.public Course(String courseName) {
20.this.courseName = courseName;
21.}
22. 
23.@Id
24.@GeneratedValue
25.@Column(name="COURSE_ID")
26.public long getCourseId() {
27.return this.courseId;
28.}
29. 
30.public void setCourseId(long courseId) {
31.this.courseId = courseId;
32.}
33. 
34.@Column(name="COURSE_NAME", nullable=false)
35.public String getCourseName() {
36.return this.courseName;
37.}
38. 
39.public void setCourseName(String courseName) {
40.this.courseName = courseName;
41.}
42. 
43.}
Now create the hibernate configuration file with the Student and Course class mapping.

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 class="com.vaannila.student.Student" />
16.<mapping class="com.vaannila.student.Course" />
17.</session-factory>
18.</hibernate-configuration>
Create the Main class to run the example.

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<Course> courses = new HashSet<Course>();
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.}
On executing the Main class you will see the following output.
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.

No comments:

Post a Comment