Join for uni-direction one-to-many relation with criteria api

14 Views Asked by At

I have to classes/tables

@Entity
class HistoricData {
  private String name;
  private Integer code;

  @OneToMany(
    fetch = FetchType.EAGER,
    cascade = CascadeType.ALL,
    orphanRemoval = true
  )
  @JoinColumn(name="historic_data_id")
  private List<HistoricDataYear> dataYears;
}

@Entity
class HistoricDataYear {
  private Long id;
  private int year;
  private BigDecimal value;
}

There is uni-direction One-To-Many link, so in table historic_data_year I have foreign key to table historic_data with name historic_data_id.

And I want to write select using Criteria to get list of HistoricDataYear with some filtration by fileds in HistoricData.

To do that, I have to write something like

Root<HistoricDataYear> root = criteria.from(HistoricDataYear.class);
root.join("historicData", JoinType.INNER);   <==== PROBLEM !!!! There is no field HistoricData in HistoricDataYear class

and after that I have to somehow join with HistoricData, but I don't know how to do it because class HistoricDataYear doesn't have field that refers to HistoricData (there is reference only on db level by historic_data_id column). Is it possible to make this kind of join?

0

There are 0 best solutions below