使用JoinColumns

时间:2019-02-13 18:30:54

标签: hibernate many-to-one bidirectional

父实体的注释如下:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "a")
private Set<Child> b;

子实体的注释如下:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "col_A")
private Parent a;

通过存储库执行对Parent的联接FETCH查询时,例如:

@Query("select a " +
            "from Parent a " +           
            "left join FETCH a.b " +
            "where a.prop = <some_value> ")

查询按预期工作,执行一个返回A及其子项的大联接。但是,如果我们按照以下方式更改Child的注释,则查询开始执行1 + N:

@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({
           @JoinColumn(name = "COL_A", referencedColumnName = "COL_A"),
            @JoinColumn(name = "COL_B", referencedColumnName = "COL_B")
    })
    private Parent a;

COL_A是父级的主键,而COL_B是用作分区的非键列

0 个答案:

没有答案