Hello,
I have a many-to-many hibernate annotations project.
There are 3 tables - Course (courseid, coursename), Student (studentid, studentname) and student_course (studentid, courseid).
There are 2 entities - Course and Student. Student entity is the owner.
I have coded the 2 entities as follows:
Code:
package com.statestreet.pojo;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.JoinColumn;
@Entity
@Table(name = "STUDENT")
public class Student implements java.io.Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private int studentId;
private String studentName;
private Set<Course> courses;
public Student() {
}
public Student(int id, String name) {
studentId = id;
studentName = name;
}
public Course addCourse(Course inCourse) {
this.getCourses().add(inCourse);
inCourse.addStudent(this);
return inCourse;
}
public Course removeCourse(Course inCourse) {
getCourses().remove(inCourse);
inCourse.removeStudent(this);
return inCourse;
}
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name="student_course", joinColumns= {
@JoinColumn(name="STUDENTID", nullable = false, updatable = false) },
inverseJoinColumns= {@JoinColumn(name="COURSEID",
nullable = false, updatable = false) })
public Set<Course> getCourses(){
return this.courses;
}
public void setCourses(Set<Course> courses) {
courses = courses;
for (Course c : courses) {
c.addStudent(this);
}
}
@Id
@Column(name="STUDENT_ID", nullable=false)
public int getStudentId() {
return studentId;
}
public void setStudentId(int id) {
this.studentId = id;
}
@Column(name="STUDENT_NAME", nullable=false)
public String getStudentName() {
return this.studentName;
}
public void setStudentName(String name) {
this.studentName = name;
}
}
Code:
package com.statestreet.pojo;
import java.util.HashSet;
import java.util.Set;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.JoinColumn;
@Entity
@Table(name="COURSE")
public class Course implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private int courseId;
private String courseName;
private Set<Student> students;
public Course() {
}
public Course(int courseId, String courseName)
{
this.courseId = courseId;
this.courseName = courseName;
students = new HashSet<Student>();
}
@Id
@Column(name="COURSE_ID", nullable=false)
public int getCourseId()
{
return courseId;
}
public void setCourseId(int courseId)
{
this.courseId = courseId;
}
@Column(name="COURSE_NAME", nullable=false)
public String getCourseName()
{
return courseName;
}
public void setCourseName(String courseName)
{
this.courseName = courseName;
}
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "courses")
public Set<Student> getStudents()
{
return students;
}
public void setStudents(Set<Student> students)
{
this.students = students;
}
public void addStudent(Student s1) {
this.getStudents().add(s1);
}
public void removeStudent(Student s1) {
this.getStudents().remove(s1);
}
}
At the DAO layer, I am adding a new student like so:
Code:
public Student addStudent(Student std)
{
Student student = null;
try
{
Transaction transaction = session.beginTransaction();
session.save(std);
transaction.commit();
sessionFactory.close();
} catch (Exception e) {
e.printStackTrace();
}
return student;
}
From the client, I call the DAO and pass the new student like this:
Code:
public static void main(String[] args) {
Student student = new Student();
student.setStudentId(102);
student.setStudentName("Jayashree Ravi");
Set<Course> stdcourses = new HashSet<Course>();
Course course1 = new Course(1, "CORE JAVA");
Course course2 = new Course(2, "J2EE-Hibernate");
stdcourses.add(course1);
stdcourses.add(course2);
student.setCourses(stdcourses);
StudentDao stdDao = new StudentDao();
stdDao.addStudent(student);
}
Now, my problem is that while data is getting inserted into the student table, although I have defined the mapping as stated above in both Student and Course entities, as well as defined foreign key constraints in the database level, when I save the student entity, only the student table in the database shows the new row. The student_course table and Course table do not get populated at all. I tried working around this for a long time but no success. What could I be doing wrong?