Annotation @Manytomany in the same entity/table

Asked

Viewed 397 times

3

My Class:

@Entity
public class Report extends  AbstractModel implements IReport {

@Column
private String nome;

@Column
private byte[] arquivoJrxml;


@ManyToMany
private List<Report> subReports;

My doubt:

My Report class, has several Subreports, that is, many to many, because several Subreports are in several reports, my doubt is due to the annotation, taking into account that a Subreport is an object of type report, my annotation needs more attributes, I am correct using Subreport and Report in the same Class/Table?

  • I didn’t see it in your code: "taking into account that a Subreport is a report object", I only saw the entity Report. There is an entity SubReport? Seeing only the code shown, it seems that is correct yes, but I would not use @Manytomany because I like to map the intermediate table, if it is not too "magical" and less flexible.

  • Indeed Dherik, thank you for the considerations

  • Answer your question, there is no entity Subreport

1 answer

2


...I am correct using Subreport and Report in the same Class/Table?

The answer to this question depends on your business rules, you need to analyze the whole context, and define whether you need a Subreport class or not. For example, if the two classes had the same attributes and are used in the same context, it is a sign that you only need one class. But if every Subreport has data(attributes) that is not present in Report, it may be a sign that Subreport is a subclass or that it contains the Report class.

...taking into account that a Subreport is a report object, my annotation needs more attributes?

The Many to Many annotation has some attributes that are always needed(jointable, joinColums, ...), but the big secret in this field is that you will need two collections in the same class, one that represents the Subreports of a Report, and one that represents the Reports of a Subreport, after all when they are two different classes, each of them has a collection that represents the Many to Many relationship. Ex.:

@ManyToMany
@JoinTable(name="report_subreport",
 joinColumns=@JoinColumn(name="id_subreport"),
 inverseJoinColumns=@JoinColumn(name="id_report")
)
private List<Report> subReports;

@ManyToMany
@JoinTable(name="report_subreport",
 joinColumns=@JoinColumn(name="id_report"),
 inverseJoinColumns=@JoinColumn(name="id_subreport")
)
private List<Report> reports;

In this code example I considered that the union table is called report_subreport, and that this table has two columns: id_report and id_subreport, which are actually a reference for the primary key of the report table, as if it were a union table of two different tables, but in this case it is the union of a table that relates to itself, both id_report and id_subreport point to the table report, but to records other than this table, where one of them is the report and the other is the subreport.

Browser other questions tagged

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