如何在JPA中的@Query中加入两个或多个@ManyToMany fiels

时间:2016-06-22 06:55:14

标签: jpa join many-to-many inner-join

我有:

public class Foo {

    @ManyToMany
    private Set<Bar> bars;

    @ManyToMany
    private Set<Baz> bazs;

    @Column(name = "text_data")
    private String txt;

}

然后我有:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface FooRepository extends JpaRepository<EntityPermission,Long> {

    @Query("SELECT DISTINCT f.txt FROM Foo f JOIN f.bars bars <...> ")
    Set<String> getFooTxts(@Param("bar") Bar bar, @Param("baz") Baz baz);

}

问题是:    我应该改为:&lt; ...&gt; 从所有连接到指定条形栏 Foo.txt >和 Baz baz

1 个答案:

答案 0 :(得分:0)

我正在考虑这样的事情:

PRODUCT ID

但它看起来不太好,不确定它是否会起作用:(

//编辑

它不能正常工作

得到:

    @Query("SELECT DISTINCT f.txt 
    FROM Foo f JOIN f.bars bars 
    WHERE bars.id = :bar.id 
    AND f.id IN ( 
        SELECT f2.id 
        FROM Foo f2 JOIN f2.bazs bazs 
        WHERE bazs.id = :baz.id )")

//编辑

看起来像

Caused by: java.lang.IllegalArgumentException: 
Validation failed for query for method public abstract ...

此查询已成功初始化 - 但我不确定它是否正常工作