JPA annotation @OneToMany or ManytoOne?

2

I have the Report Class

public class Report{

    private String nome;

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

    getters e setters...
}

My question is how to do the annotation correctly, this way I'm getting this:

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

Since the SubReport list is an array of the Report class itself, that is, it is the same table.

How to proceed?

    
asked by anonymous 16.01.2018 / 13:08

1 answer

3

If A Report has VARIOUS subreports then the annotation to use is @OneToMany

public class Report{

    private String nome;

    @OneToMany
    private List<Report> subReports

    getters e setters...
}

A List can not be considered as A , so the @OneToOne and @ManyToOne annotations do not apply in this case.

Since subreports is a two-way relationship for the same report class then you must map both sides of the relationship, @JoinColumn should be applied to 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...
}
    
16.01.2018 / 13:19