I have the following entity structure (the DB is given):
@Data
@Entity
public class Report {
@EmbeddedId
private ReportId id;
private int numberOne;
private int numberTwo;
}
Within my reports I need the complete site. But it might be deleted (no foreign key constraint).
@Data
@Entity
public class Site {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String place;
private String name;
}
For every site and day there is a report. I am not interested in reports from deleted sites.
@Embeddable
@Data
public class ReportId implements Serializable {
@NotFound(action = NotFoundAction.IGNORE) //without this we get EntityNotFoundException for deleted sites
@Fetch(FetchMode.JOIN)
@OneToOne
@JoinColumn(name = "site_id", referencedColumnName = "id")
private Site site;
@Column(name = "day")
private LocalDate date;
}
What I want is that a "selectAll()" only returns reports that are linked to existing sites (lookup by site id is returning not null).
As you see I already tried @NotFound(action = NotFoundAction.IGNORE) but this creates entries with site = null.
I think what I need is a left outer join of site but it seems @Fetch(FetchMode.JOIN) is just ignored - I still see 2 selects per report (first selecting the report 2nd fetching the site by id).
I also tried to put these annotations on to @EmbeddedId column in Report but this is not working (@NotFound is also ignored there).
Is there a way to ignore all reports without an existing site? - I don't want to write the queries manually. I want to use Spring Data repository methods like - findAll().