Gabriel
Gabriel

Reputation: 27

JPA ManyToMany does not insert into join table when create

I'm using JPA 2.1, mysql.

Entity Usuario

@Entity
@Table(name = "USUARIO")
public class Usuario {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID_USUARIO")
    private Integer id;

    @Column(name = "NOMBRE", length = 30)
    private String nombre;

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "integrantes", cascade = {CascadeType.ALL})
    private List<NucleoFamiliar> nucleosFamiliares;

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "integrantes", cascade = {CascadeType.ALL})
    private List<GrupoJaas> gruposJaas;

    ... getters setters
}

Entity GrupoJaas

@Entity
@Table(name = "GRUPO_JAAS")
public class GrupoJaas {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID_GRUPO_JAAS")
    private Integer id;

    @Column(name = "NOMBRE", length = 30)
    private String nombre;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "INTEGRANTE_GRUPO_JAAS", joinColumns = {
        @JoinColumn(name = "ID_GRUPO_JAAS") }, inverseJoinColumns = @JoinColumn(name = "ID_USUARIO"))
    private List<Usuario> integrantes;

    ... getters setters
}

Entity NucleoFamiliar

@Entity
@Table(name = "NUCLEO_FAMILIAR")
public class NucleoFamiliar {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID_NUCLEO_FAMILIAR")
    private Integer id;

    @Column(name = "NOMBRE", length = 100)
    private String nombre;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "INTEGRANTE_NUCLEO_FAMILIAR", joinColumns = {
        @JoinColumn(name = "ID_NUCLEO_FAMILIAR") }, inverseJoinColumns = @JoinColumn(name = "ID_USUARIO"))
    private List<Usuario> integrantes;

    ... getters setters
}

When I create a new "NucleoFamiliar" with a non-empty list of "integrantes", jpa executes 2 sentences as expected:

insert into NUCLEO_FAMILIAR
insert into INTEGRANTE_NUCLEO_FAMILIAR

When I create a new "Usuario" with a non-empty list of "gruposJaas", jpa executes only one sentence:

insert into USUARIO

Why is not executing insert into INTEGRANTE_GRUPO_JAAS!!???

Both codes (create "NucleoFamiliar" and create "Usuario") are almost the same, I mean, they are almost identical.

Please help, I spent days watching everything and I haven't found a solution yet (I'm using jpa 2.1)

Upvotes: 2

Views: 7031

Answers (1)

Zulfi
Zulfi

Reputation: 586

In Usario class by using the mappedBy attribute in the @ManyToMany association mapping you have specified owner of the two associations.

In the below two association mapping

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "integrantes", cascade =  {CascadeType.ALL})
private List<NucleoFamiliar> nucleosFamiliares;

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "integrantes", cascade = {CascadeType.ALL})
private List<GrupoJaas> gruposJaas;` 

by using mappedBy attribute in the relationship you have effectively specified that the owner of these association is GrupoJass and NucleoFamiliar.

Now in a relationship the owner only can update the relationship status. Hence when you save NucleoFamiliar it also updates the relationship that is the JoinTable.

Upvotes: 5

Related Questions