Difficulty with JPA annotations for entity relationships

2

I have the following relationships: CategoriaPeca , SubcategoriaPeca and Peca . A CategoryPart can have several SubcategoryPart , and a SubcategoryPart can have several CategoryPart , a part can only be in one SubcategoryPeca and only in a CategoriaPeca . The following annotations were created in an attempt to create this relationship.

@Entity
@Table
public class CategoriaPeca implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    @Column
    private String descricao;

    @OneToMany(mappedBy = "categoriaPeca")
    private List<Peca> peca;

    @ManyToMany(mappedBy = "categorias", cascade = CascadeType.ALL)
    private List<SubcategoriaPeca> subcategorias;

@Entity
@Table
public class SubcategoriaPeca implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    @Column
    private String descricao;

    @OneToMany(mappedBy = "subcategoriaPeca", fetch = FetchType.EAGER)
    private List<Peca> pecas;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "categoriapeca_subcategoriapeca",
                joinColumns = {@JoinColumn(name = "categoriapeca_id")},
                inverseJoinColumns = {@JoinColumn(name = "subcategoriapeca_id")})
    private List<CategoriaPeca> categorias;

@Entity
@Table
public class Peca implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @NotEmpty
    @Column(name = "cod_item", nullable = false, length = 14)
    private String codigo;

    @ManyToOne(fetch = FetchType.LAZY)
    private SubcategoriaPeca subcategoriaPeca;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "categoriapeca_id", referencedColumnName = "id")
    private CategoriaPeca categoriaPeca;

What I need is to select each CategoryPat and show me each SubcategoryPart , and each Peca where category.id is equal to the selected category and subcategory_id is equal to referenced .

Ex:

  

I have the Motor category that has the subcategories Retentor and Cruzeta,   I have the brake category that has the subcategories   Bearing, I have the part that has the subcategory Retainer and category   Brake, The part should only appear when selecting Brake >   Retainer

    
asked by anonymous 06.09.2018 / 14:37

0 answers