JPA many to many relation not inserting into generated table

jpa many-to-many with extra column
jpa many-to-many bidirectional example
hibernate many-to-many delete problem
spring data jpa many to-many query
jpa many to many as one to many
hibernate many-to-many without join table
jpa join table one-to-many
jpa many to-many not loading

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.

Here's how I'm declaring this using JPA annotations:

Professor.java

@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<Aluno> alunoList;

    // getters and setters
}

Aluno.java

@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<Professor> professorList;

    // getters and setters
}

And here is the service layer to insert into database:

@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);

    ...
}

In this case, please consider that I already have an entry with id "1" for Professor.

As I said, it does write on Aluno and Professor table but does NOT write anything into ALUNO_PROFESSOR table.

I've already taken a look at these three kind of similiar questions but none of them could help me:

Hibernate and Spring: value of many-to-many not inserted into generated table

JPA many-to-many persist to join table

How to persist @ManyToMany relation - duplicate entry or detached entity

EDIT - Adding more code snippets

JpaAlunoDao.java

@Repository
public class JpaAlunoDao implements AlunoDao {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void persist(Aluno aluno) {
        em.persist(aluno);
    }
}

JpaExercicioDao.java

@Repository
public class JpaExercicioDao implements ExercicioDao {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void persist(Exercicio exercicio) {
        em.persist(exercicio);
    }
}

Try this:

public class Professor {
  @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinTable(name = "ALUNO_PROFESSOR",
        joinColumns = @JoinColumn(name = "idProfessor", referencedColumnName = "idProfessor"),
        inverseJoinColumns = @JoinColumn(name = "idAluno", referencedColumnName = "idAluno"))
  private List<Aluno> alunoList;
}

public class Aluno {
  @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinTable(name = "ALUNO_PROFESSOR",
        joinColumns = @JoinColumn(name = "idAluno", referencedColumnName = "idAluno"),
        inverseJoinColumns = @JoinColumn(name = "idProfessor", referencedColumnName = "idProfessor"))
  private List<Professor> professorList;
}

This will ensure that the metadata for the many-to-many relationship is available on both the entities and that operations on either side of the relationship are cascaded to the other side.

I also suggest replacing FetchType.EAGER with FetchType.LAZY for better performance because this has the potential of loading a very large dataset.

The best way to use the @ManyToMany annotation with JPA and , Learn the best way to map a ManyToMany association when using JPA As simple as JPA annotations might be, it's not always obvious how many-to-many database association includes two parent tables in this bidirectional relationship, the Post entity owns the association. INSERT INTO post_tag. JPA many to many relation not inserting into generated table Tag: mysql , spring , hibernate , jpa , many-to-many 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.


I have the same issue. I swapped where the full mapping declare to the class that we will use save() function on. In your case:

public class Aluno {
  @ManyToMany(fetch = FetchType.EAGER)
  @JoinTable(name = "ALUNO_PROFESSOR",
        joinColumns = @JoinColumn(name = "idAluno"),
        inverseJoinColumns = @JoinColumn(name = "idProfessor")
  private List<Professor> professorList;
}

public class Professor {
  @ManyToMany(fetch = FetchType.EAGER, mappedBy = "professorList",)  
  private List<Aluno> alunoList;
}

and It worked fine.

The best way to map a many-to-many association with extra , The JPA many to many association can take extra columns in which case you you can use the @ManyToMany JPA annotation and, therefore, hide the join table. the best way to map a one-to-many database relationship that requires a collection of INSERT INTO post_tag (created_on, post_id, tag_id). The many-to-many relationship is implemented using a third table called post_tags which contains the details of posts and their associated tags. Let’s now create a project from scratch and learn how to go about implementing such many-to-many relationship using JPA and Hibernate.


Its not necessary to set many-to-many relationship on both entities.

Just remove session.setFlushMode(FlushMode.MANUAL);

By default HibernateTemplate inside of Spring set FlushMode.MANUAL

this is source code from HibernateTemplate.

 if (session == null) {
            session = this.sessionFactory.openSession();
            session.setFlushMode(FlushMode.MANUAL);
            isNew = true;
        }

Java persistence with JPA and Hibernate, Part 2: Many-to-many , Many-to-many relationships and cascade type strategies in JPA and specifies that the table's primary key will be automatically generated. In case of a many-to-many relationship, both sides can relate to multiple instances of the other side. Note, that it's possible for entity types to be in a relationship with themselves. For example, when we model family trees: every node is a person, so if we talk about the parent-child relationship, both participants will be a person.


Many-To-Many Relationship in JPA, Learn how to model a many-to-many relationship in Java using JPA. Learn how table names are generated by default and how to override that behavior. The only problem is that we cannot add a property to a relationship  This tutorial will walk you through the steps of mapping a JPA and Hibernate Many to Many bidirectional entity relationships example with Spring Boot, Spring Data JPA, Lombok, MySQL and Docker What you will need JDK 8+ or OpenJDK 8+ Maven 3+ MySQL Server 5+ or Docker CE 18+ Init


JPA / Hibernate Many to Many Mapping Example with Spring Boot , JPA, Hibernate, Spring boot Many to Many Mapping Example Table Structure how to go about implementing such many-to-many relationship using JPA and Hibernate. Hibernate # The SQL dialect makes Hibernate generate better SQL for SQL : insert into posts (content, description, last_updated_at,  In a one-to-many relationship between Table A and Table B, each row in Table A is linked to 0, 1 or many rows in Table B. Let us consider the above example. If Employee and Department is in a reverse unidirectional manner, relation is Many-To-One relation. Create a JPA project in eclipse IDE named JPA_Eclipselink_OTM. All the modules of this


Most efficient way to map a @OneToMany relationship with JPA and , The many side of @ManyToOne bidirectional relationships must not This above code will generate 2 tables Company(company_id,name) and insert into vehicle (id, name, person_id) values (1, "ford", null); insert into  For a simple many-to-many database relationship, you can use the @ManyToMany JPA annotation and, therefore, hide the join table. However, sometimes you need more than the two Foreign Key columns in the join table, and, for this purpose, you need to replace the @ManyToMany association with two bidirectional @OneToMany associations.