Mota
Mota

Reputation: 25

JPA problem with @ManyToMany annotation using @JoinTable

I am trying to create a relationship between two entities: user and role.

The relational table between the entities, users_roles, is successfully generated, but in addition the two columns that make up this table, id_user and id_role, are also added to the role table and I'm not sure if this is expected...

role entity:

@Table(name = "role")
@Entity
public class Role extends AbstractEntity {

    private static final long serialVersionUID = 1L;

    public static final int PAPEL_USUARIO = 1;

    public static final int PAPEL_ADMIN = 2;

    public static final int PAPEL_PLANTONISTA = 3;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ROLE")
    @SequenceGenerator(name = "SEQ_ROLE", sequenceName = "id_seq_role", allocationSize = 1)
    private Long id;

    @Column(name = "description", columnDefinition = "varchar(500)", nullable = true)
    private String description;

    @ManyToMany(mappedBy = "roles")
    private List<User> users;

    public Role() {

    }

    public Role(Long id) {
        this.id = id;
    }

    @Override
    public Long getId() {
        // TODO Auto-generated method stub
        return id;
    }

    @Override
    public void setId(Long id) {
        // TODO Auto-generated method stub
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

user entity:

package br.ufrn.imd.hospital.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.Valid;

@Table(name = "\"user\"")
@Entity
public class User extends AbstractEntity {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_USER")
    @SequenceGenerator(name = "SEQ_USER", sequenceName = "seq_user", allocationSize = 1)
    private Long id;

    @Valid
    @OneToOne(cascade= {CascadeType.MERGE,CascadeType.PERSIST})
    private Person person;

    @ManyToMany
    @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "id_user", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "id_role", referencedColumnName = "id"))
    private List<Role> roles;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
}

Upvotes: 0

Views: 696

Answers (2)

Mota
Mota

Reputation: 25

The problem was the use of the optonal referencedColumnName = "id" on joinColumns and inverseJoinColumns inside the @JoinTable annotation... I removed and the issue was fixed!

Upvotes: 0

Pandit Biradar
Pandit Biradar

Reputation: 1877

This is expected behavior

@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "id_user", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "id_role", referencedColumnName = "id"))

This tells that we are creating the new join table , which joins the two columns from 2 tables mapped id_user from User and id_role from role table

You can check

https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/JoinTable.html

Upvotes: 1

Related Questions