title: Java Spring - ManyToMany Call Error - Object references an unsaved transient instance
body: <p>I have a problem with a repository call to an object's attribute, which is part of a <code>Many-To-Many</code> relationship.</p>
  
  <p>I am trying to get all the lecturers assigned to a specific course (getting all the courses for a specific lecturer works very well). But when I am calling the method from the repository below I get the 
  error: </p>
  
  <p><code>org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: app.model.Course</code></p>
  
  <p>The <code>CascadeType</code> is set for both parts of the relationship.</p>
  
  <p>I have the following configuration:</p>
  
  <p><strong>Lecturer</strong></p>
  
  <pre><code>@Entity
  @Table(name = "LECTURERS")
  @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  public class Lecturer extends AbstractUser {
  
      @ManyToMany(fetch = FetchType.EAGER, targetEntity = Course.class, cascade = CascadeType.ALL)
      @Fetch(FetchMode.SELECT)
      @JoinTable(name = "COURSE_OWNERSHIPS",
              joinColumns = {@JoinColumn(name = "lecturer_id")},
              inverseJoinColumns = {@JoinColumn(name = "course_id")})
      @JsonSerialize(using = CustomCourseListSerializer.class)
      private List&lt;Course&gt; courses = new ArrayList&lt;&gt;();
  
      ...
  }
  </code></pre>
  
  <p><strong>Course</strong></p>
  
  <pre><code>@Entity
  @Table(name = "COURSES")
  @JsonIdentityInfo(
          generator = ObjectIdGenerators.PropertyGenerator.class,
          property = "id")
  public class Course implements Item, Serializable {
  
      ...
  
      @ManyToMany(mappedBy = "courses", fetch = FetchType.EAGER, targetEntity = Lecturer.class, cascade = CascadeType.ALL)
      @Fetch(value = FetchMode.SUBSELECT)
      @JsonSerialize(using = CustomLecturerListSerializer.class)
      private List&lt;Lecturer&gt; lecturers;
  
      ...
  }
  </code></pre>
  
  <p><strong>Lecturer Repository</strong></p>
  
  <pre><code>public interface LecturerRepository extends PagingAndSortingRepository&lt;Lecturer, Long&gt; {
  
      ...
  
      Page&lt;Lecturer&gt; findAllByCourses(Course course, Pageable pageable);
  
  }
  </code></pre>
  
  <p><strong>Edited</strong></p>
  
  <p><strong>Course Service Method</strong></p>
  
  <pre><code>@Override
      public Page&lt;Lecturer&gt; getLecturers(Course course, int page) {
          return lecturers.findAllByCourses(course, new PageRequest(page, 10));
      }
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p><strong>Full course service method</strong></p>
  
  <pre><code>  @Override
      public Page&lt;LecturerDto&gt; getLecturers(CourseDto course, int page) {
          Type listType = new TypeToken&lt;Page&lt;LecturerDto&gt;&gt;() {}.getType();
          return modelMapper.map(lecturers.findAllByCourses(modelMapper.map(course, Course.class), new PageRequest(page, 10)), listType);
      }
  </code></pre>
  
  <p>I've tried a few things but I can't make it work. Do you have any idea why I encounter this? I would like to do this because I want the lecturers of a course paginated.</p>
  
  <p>Thank you.</p>