Tuesday, August 20, 2013

Hibernate 4 - One to Many mapping with Annotations (LAZY loading example)

 the technologies i ma using for this example.

Hibernate 4
testNG for TDD
MySQL
Maven

In this example, we are going to use our Lecturer and Course example again the entity relationship will be decided based on the following assumption.

Assumption:
 Lecturer can teach many number of courses.

Therefore the relationship between two entities can be described as follows.

  •  relationship from Lecturer to Course is One-To-Many (lecturer can teach many number of courses) 
  •  relationship from Course to Lecturer is Many-To-One (many courses will be taught by one lecturer)
Refer the below example.

Lecturer.java
package com.chathurangaonline.examples.model;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Entity
@Table(name = "Lecturer")
public class Lecturer implements Serializable{

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;

    @OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = "lecturer")
    private List courseList;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List getCourseList() {
        return courseList;
    }

    public void setCourseList(List courseList) {
        this.courseList = courseList;
    }
}





Course.java
package com.chathurangaonline.examples.model;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "Course")
public class Course implements Serializable{

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;

    @Column(name = "course_name")
    private String courseName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "lecturer_id")
    private Lecturer lecturer;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public Lecturer getLecturer() {
        return lecturer;
    }

    public void setLecturer(Lecturer lecturer) {
        this.lecturer = lecturer;
    }
}




i have used the following code to check the mapping relationship  and the lazy loading.



    @Test
    public void testSave() throws Exception {

        //saving the lecturer
        Lecturer lecturer = new Lecturer();
        lecturerDao.save(lecturer);
        Assert.assertNotNull(lecturer.getId());

        //saving the course
        Course course = new Course();
        course.setCourseName("Java");
        course.setLecturer(lecturer);
        courseDao.save(course);
        Assert.assertNotNull(course.getId());

        System.out.println(" retrieving the lecturer record with id ["+lecturer.getId()+"]");
        Lecturer  lecturer2  = lecturerDao.findById(lecturer.getId());
        System.out.println("lecturer found with id ["+lecturer2.getId()+"] retrieved");
        System.out.println("course retrieving .......");
        List  courseList1 = lecturer2.getCourseList();
        System.out.println(" course list retrieved ["+courseList1.size()+"]");
    }


when i run the above code, i got the following output and seems to be that the lazy loading is also working fine.



you can get the example source code from following gitHub repository.
Get SourceCode from GitHub


Hope this will be helpful for you!

Cheers
Chathuranga Tennakoon
chathuranga.t@gmail.com
www.chathurangaonline.com


No comments:

Post a Comment