Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Unidirectional manytomany not persisting

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!

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

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) {
} else {
students.set(index, student);


<pre name="code" class="core">
public class Student implements Comparable, Serializable {
private int id;

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