Join 和 JoinSet 之间的 JPA 差异

Gae*_*ano 5 java jpa criteria-api jpa-2.0 jakarta-ee

正如标题所说,我想知道这两种方法之间的区别。

具体来说,我想知道以下之间的区别:

join(String arg) AND joinSet(String arg)

因为即使属性是一个集合,我也可以使用join(String arg),但不是相反,即在不是集合的属性上使用joinSet(String arg)

谢谢。

Con*_*Man 4

join 方法用于在单个属性上创建内部联接,即一对一关系。

   /*Create an inner join to the specified single-valued attribute.
    Parameters:
    attribute target of the join
    Returns:
    the resulting join*/
74 
75     <Y> Join<X, Y> More ...join(SingularAttribute<? super X, Y> attribute);
Run Code Online (Sandbox Code Playgroud)

而 joinSet 方法用于为一组属性创建内部联接,即一对多关系。

 /*Create an inner join to the specified Set-valued attribute.
    Parameters:
    attributeName name of the attribute for the target of the join
    Returns:
    the resulting join
    Throws:
    java.lang.IllegalArgumentException if attribute of the given name does not exist*/
182
183    <X, Y> SetJoin<X, Y> More ...joinSet(String attributeName);  
Run Code Online (Sandbox Code Playgroud)

但是,如果查看方法的返回类型,就会发现 join 返回类型 Join,而 joinSet 返回类型 SetJoin,后者实现了 Join。这意味着实现应用程序完全有可能放入一些逻辑来检测您是否正在尝试加入集合或单个属性,并在必要时将过程转发到 joinSet 方法。在不知道您正在使用什么实现的情况下,我无法对此进行进一步评论。

源代码在 grep 代码中找到