Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate: Adding a new element to association list does not persist
    primarykey
    data
    text
    <p>I have a ManyToMany association between two Entities: COURSE and STUDENT. They are associated through a STUDENT_COURSE_MAP association table.</p> <p><strong>Course.java:</strong></p> <pre><code>@Entity @Table(name="COURSE") public class Course implements java.io.Serializable { @ManyToMany( mappedBy="courses", cascade={CascadeType.PERSIST, CascadeType.MERGE} ) private List&lt;Student&gt; students = Collections.emptyList(); // Rest of the class, getters, setters etc } </code></pre> <p><strong>Student.java:</strong></p> <pre><code>@Entity @Table(name="STUDENT") public class Student implements java.io.Serializable { @ManyToMany( targetEntity=Course.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE } ) @JoinTable( name="STUDENT_COURSE_MAP", joinColumns=@JoinColumn(name="STUDENT_REF"), inverseJoinColumns=@JoinColumn(name="COURSE_REF") ) private Set&lt;Course&gt; courses = Collections.emptySet(); // Rest of the class, getters, setters etc } </code></pre> <p>I want to add a student (id=11) list for one particular course (id=77) as follows (transaction is already begun):</p> <pre><code>// Create new list and add a student to it Student student_11 = (Student) session.get(Student.class.getName(), 11); List&lt;Student&gt; studentsList = new ArrayList&lt;Student&gt;(1); studentsList.add(student_11); // Now, set the list of students in the course Course course_77 = (Course) session.get(Course.class.getName(), 77); course_77.setStudents(studentsList); </code></pre> <p>I save it using the following code:</p> <pre><code>session.flush(); session.getTransaction().commit(); </code></pre> <p>However, the association - course_77 and student_11 - does not get persisted to the database. There are no insert statements generated in the hibernate log.</p> <p>I even tried calling <code>session.saveOrUpdate(course_77)</code>. Only the following is logged when <code>saveOrUpdate</code> is called:</p> <pre><code>allowing proxied method [saveOrUpdate] to proceed to real session persistent instance of: org.whatra.tradecog.db.entity.Artist ignoring persistent instance object already associated with session: [Course#77] </code></pre> <p>Hints/tips/solutions are much appreciated.</p> <hr> <p><strong>Note 1:</strong> The whole persistence thing works just fine for me when I create new Course and Student objects and save. What does not work is when I retrieve existing Course and existing Student objects and add a new association between them.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload