Difficulty with JPA annotations for entity relationships

Asked

Viewed 42 times

2

I have the following relationships: Categoriapeca, Subcategoriapeca and Peca. One Categoriapeca may have several Subcategoriapeca, and a Subcategoriapeca may have several Categoriapeca, a piece can only be in one Subcategoriapeca 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 that at the time of selecting each Categoriapeca be shown each Subcategoriapeca referent, and each Peca where the.id category is equal to the selected category and subcategory_id is equal to Subcategoriapeca referent.

Ex:

I have the Engine category that has the subcategories Seal and Cross, I have the Brake category that has the subcategories Seal and Bearing, I have the piece that has the subcategory Retainer and category Brake ,The part shall appear only when selecting the category Brake > Retainer

  • But what’s going on? Isn’t it working? Does it show an error message? Instantly, I think @Joincolumn is missing in the subcategoryPeca de Peca.

No answers

Browser other questions tagged

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