Notes JPA @Onetomany or Manytoone?

Asked

Viewed 1,072 times

2

I own the Report Class

public class Report{

    private String nome;

    @ManyToOne
    @JoinColumn( name = "idpai", referencedColumnName = "id" )
    private List<Report> subReports

    getters e setters...
}

My doubt is how to carry out the annotation in the correct way, in this way as above I am receiving:

org.hibernate.AnnotationException: @OneToOne or @ManyToOne on br.com.koinonia.habil.model.user.Report.subReportProvider references an unknown entity: java.util.List

The Subreport list being an array of the Report class itself, that is, it is the same table.

How to proceed?

1 answer

3


If ONE Report has SEVERAL subreports so the annotation that should be used is @OneToMany

public class Report{

    private String nome;

    @OneToMany
    private List<Report> subReports

    getters e setters...
}

One List cannot be considered as ONE, then the notes @OneToOne and @ManyToOne do not apply in this case.

Since subreports are a bidirectional relationship for the same reporting class so you should map both sides of the relationship, the @JoinColumn shall be applied in the column representing the parent report.

public class Report {

    private String nome;

    @ManyToOne
    @JoinColumn(name="idpai")
    private Report masterReport;

    @OneToMany(mappedBy="masterReport")
    private List<Report> subReports

    getters e setters...
}

Browser other questions tagged

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