How to make a JPA query with mappings @Manytomany

Asked

Viewed 63 times

2

Good morning.

I’m having trouble performing a query in the database where my Employee entity has 2 mappings @Manytomany and 1 mappings @Manytoone. When performing a query the Employee and Projects data are returned without major problems. The relational attributes of Manytomany being a problem. To make the insertion is occurring ok. To search the base for a problem. Can someone please help? Thank you

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

Skill class

@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(){}

}
  • I could post on the answer, where you make the flame of Employe and what would be the errors occurred

1 answer

0

Making 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);
  • What I wish Kaique is that when I search all registered Employee, return along his Kill and the certifications that he owns

  • When debugging gave the following error:

  • 2018-12-22 11:51:32.069 WARN 6064 --- [nio-8080-exec-2] .w.s.m.s.Defaulthandlerexceptionresolver : Failure while trying to resolve Exception [org.springframework.http.converter.Httpmessagenotwritableexception] java.lang.Illegalstateexception: Cannot call sendError() after the Response has been Committed

  • can you run List<Employee> findAll()? From the Crudrepository interface

  • With you yes, but the above error occurs.

  • Employee and Project data returns normal. What is not coming is Skill and Certification data

  • Tente trocar isso inverseJoinColumns = @JoinColumn(name = "id_skill") por inverseJoinColumns = @JoinColumn(name = "skill_id_skill") e também joinColumns = @JoinColumn(name = "employee_id_employee"),

  • It didn’t help either Kaique

  • Try configuring the manytomany relation in get and set methods as in the following example: link In case it still doesn’t work I think it will be the way to create a column Mind to relate the two.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.