• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unidirectional manytomany not persisting

 
Ali Hussain
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have unidirectional manytomany relation that is not working. Teacher has a reference (collection) for students, but the student does not have reference to the Teacher.

When I merge(teacher) like this getJpaTemplate().merge(teacher), with a new student added to the collection, the relation is not updated in the join table (teacher_student) and there are no exceptions either. However both teacher and student are saved properly in their tables. I have done a lot of searching but still not able to find the solution...please help!

<blockquote>code:
<pre name="code" class="core">
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Teacher implements Comparable, Serializable {

@Id
protected TeacherId teacherId;

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@BatchSize(size = 3)
protected List<Student> students;

public addOrUpdateStudent(Student student) {
if (students == null) {
students = new ArrayList<Student>();
}
int index = students.indexOf(student);
if (index == -1) {
students.add(student);
} else {
students.set(index, student);
}

}
...
}
</pre>
</blockquote>

<blockquote>code:
<pre name="code" class="core">
@Entity
public class Student implements Comparable, Serializable {
@Id
@GeneratedValue
private int id;
...
}
</pre>
</blockquote>

I am using hibernate 3.2.1.ga, postgres 8.2-504.jdbc3 and java 1.6.0.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic