How to do a JPA query with @ManyToMany mappings

2

Good morning.

I am having difficulty performing a query in the database where my Employee entity has 2 mappings @ManyToMany and 1 mapping @ManyToOne. When you perform a query, Employee and Projects data is returned without any major problems. ManyToMany relational attributes being a problem. To do the insertion is happening ok. To search the base for a problem. Can anyone help please? Thanks

@Entity
@Table(name = "employee")
@JsonView
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id_employee;

    @NotNull
    @Column(name = "name", length = 120)
    private String name;

    @NotNull
    @Column(name = "role", length = 100)
    private String role;

    @NotNull
    private Double salary;

    @NotNull
    @Column(name = "manager", length = 120)
    private String manager;

    private int gcm;

    @NotNull
    @ManyToOne
    private Projects project;

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name = "employee_skill",
        joinColumns = @JoinColumn(name = "id_employee"),
        inverseJoinColumns = @JoinColumn(name = "id_skill")
    )

    private List<Skill> skill;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "employee_certification",
        joinColumns = @JoinColumn(name = "id_employee"),
        inverseJoinColumns = @JoinColumn(name = "id_certification")
    )

    private List<Certification> certification;
}

Class skill

@Entity
@Table(name = "skill")
@JsonView
public class Skill implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id_skill;

    @NotNull
    private String descricao;

    @ManyToMany(mappedBy = "skill", fetch = FetchType.LAZY)
    private List<Employee> employee;

    public Skill(){}
}

Class certification

@Entity
@Table(name = "certification")
@JsonView
public class Certification implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id_certification;

    @NotNull
    private String descricao;

    @ManyToMany(mappedBy = "certification", cascade = CascadeType.ALL)
    private List<Employee> employee;

    public Certification(){}

    public Certification(String descricao){
        this.descricao = descricao;
    }
}

Class projects

@Entity
@Table(name = "projects")
@JsonView
public class Projects implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id_project;

    @Column(name = "name", length = 150)
    private String name;

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

    private Double valueOfProject;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "dt_Begin")
    private Calendar dtBegin;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "dt_End")
    private Calendar dtEnd;

    @OneToMany(mappedBy = "project")
    private List<Employee> employee;

    public Projects(){}

}
    
asked by anonymous 22.12.2018 / 14:03

1 answer

0

As an example of the Certification and Employee classes, I believe this can help:

List<Certification> findByEmployee_Name(String name);

Another example passing a list of names:

 List<Certification> findByEmployee_NameIn(List<String> name);
    
22.12.2018 / 14:29