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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • 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!

<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.
 
    Bookmark Topic Watch Topic
  • New Topic