title: JPA many to many relation not inserting into generated table
body: <p>I have a many-to-many relation in my project and although I'm able to write in my two Entities table, the relational table does not get anything written. </p>
  
  <p>Here's how I'm declaring this using JPA annotations:</p>
  
  <p>Professor.java</p>
  
  <pre><code>@Entity
  @Table(name = "Professor")
  public class Professor implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "idProfessor", nullable = false)
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @ManyToMany(fetch = FetchType.EAGER)
      @JoinTable(name = "ALUNO_PROFESSOR",
              joinColumns = @JoinColumn(name = "idProfessor", referencedColumnName = "idProfessor"),
              inverseJoinColumns = @JoinColumn(name = "idAluno", referencedColumnName = "idAluno"))
      private List&lt;Aluno&gt; alunoList;
  
      // getters and setters
  }
  </code></pre>
  
  <p>Aluno.java</p>
  
  <pre><code>@Entity
  @Table(name = "Aluno")
  public class Aluno implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "idAluno", nullable = false)
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @ManyToMany(mappedBy = "alunoList", fetch = FetchType.EAGER)
      private List&lt;Professor&gt; professorList;
  
      // getters and setters
  }
  </code></pre>
  
  <p>And here is the service layer to insert into database:</p>
  
  <pre><code>@Autowired
  private AlunoDao alunoDao;
  
  @Autowired
  private ProfessorDao professorDao;
  
  @RequestMapping(value = RestUriConstants.SUBMETER, method = RequestMethod.POST)
  public @ResponseBody JsonResponse submeter(@RequestBody final Aluno aluno) {
  
      Professor professor = professorDao.find(1);
      aluno.setProfessorList(Arrays.asList(professor));
      alunoDao.persist(aluno);
  
      ...
  }
  </code></pre>
  
  <p>In this case, please consider that I already have an entry with id "1" for Professor.</p>
  
  <p>As I said, it does write on Aluno and Professor table but does NOT write anything into ALUNO_PROFESSOR table.</p>
  
  <p>I've already taken a look at these three kind of similiar questions but none of them could help me:</p>
  
  <p><a href="https://stackoverflow.com/questions/13591778/hibernate-and-spring-value-of-many-to-many-not-inserted-into-generated-table">Hibernate and Spring: value of many-to-many not inserted into generated table</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19465662/jpa-many-to-many-persist-to-join-table">JPA many-to-many persist to join table</a></p>
  
  <p><a href="https://stackoverflow.com/questions/9821914/how-to-persist-manytomany-relation-duplicate-entry-or-detached-entity">How to persist @ManyToMany relation - duplicate entry or detached entity</a></p>
  
  <p><strong>EDIT - Adding more code snippets</strong></p>
  
  <p>JpaAlunoDao.java</p>
  
  <pre><code>@Repository
  public class JpaAlunoDao implements AlunoDao {
  
      @PersistenceContext
      private EntityManager em;
  
      @Transactional
      public void persist(Aluno aluno) {
          em.persist(aluno);
      }
  }
  </code></pre>
  
  <p>JpaExercicioDao.java</p>
  
  <pre><code>@Repository
  public class JpaExercicioDao implements ExercicioDao {
  
      @PersistenceContext
      private EntityManager em;
  
      @Transactional
      public void persist(Exercicio exercicio) {
          em.persist(exercicio);
      }
  }
  </code></pre>