-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to remove the record in the bridge table
PostPosted: Tue Oct 06, 2009 9:56 pm 
Newbie

Joined: Tue Oct 06, 2009 9:38 pm
Posts: 1
I am using jpa. I establish a many to many relationship on Course and Student. So I have a bridge table CourseStudent.

The code in Course,java
@Entity
@Table(name = "COURSE")
public class Course implements Serializable {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable( //
name = "COURSESTUDENT", //
joinColumns = { @JoinColumn(name = "COURSE_FK", nullable = false, updatable = false) }, //
inverseJoinColumns = { @JoinColumn(name = "STUDENT_FK", nullable = false, updatable = false) })
public Set<Student> getStudents() {
return students;
}

public void setStudents(Set<Student> students) {
this.students = students;
}
@Transient
public void addStudent(Student student) {
Set<Student> students = getStudents();
if(students == null) {
students = new HashSet<Student>(0);
setStudents(students);
}
students.add(student);
}

@Transient
public void removeStudent(Student student) {
Set<Student> students = getStudents();
if(students != null) {
students.remove(student);
}
}
Code in Student.java
@Entity
@Table(name = "STUDENT")
public class Student implements Serializable {
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "students")
public Set<Course> getCourses() {
return courses;
}

public void setCourses(Set<Course> courses) {
this.courses = courses;
}

The problem is when I want to remove one student record from the student collection of the course, no corresponding record got removed. the code is looks like:
course.removeStudent(student);

Any help would be appreciated. Thank you so much.
Note: insert and update is fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.