Spring boot jpa使用多个键值从连接映射中选择

Spring boot jpa使用多个键值从连接映射中选择,spring,hibernate,jpa,spring-boot,Spring,Hibernate,Jpa,Spring Boot,我有下面的工作查询,它根据连接映射的键和值进行选择 @Query("select e from Entity e join e.dataAttributes da where " + "(key(da) =:attrKey1 and :attrVal1 in (value(da)) )") List<Entity> findByAttrributeValues(@Param("attrKey1") String attrKey1,

我有下面的工作查询,它根据连接映射的键和值进行选择

@Query("select e from Entity e join e.dataAttributes da where " +
        "(key(da) =:attrKey1 and :attrVal1 in (value(da)) )")
List<Entity> findByAttrributeValues(@Param("attrKey1") String attrKey1,
                                      @Param("attrVal1") String attrVal1);
@Query(“从实体e中选择e加入e.dataAttributes da where”+
“(key(da)=:attrKey1和:attrVal1 in(value(da)))
列出findbyattributevalues(@Param(“attrKey1”)字符串attrKey1,
@参数(“attrVal1”)字符串attrVal1);
我想选择基于2个键和2个值,但我有困难。以下可能非常简单的尝试未返回任何结果:

 @Query("select e from Entity e join e.dataAttributes da where " +
        "(key(da) =:attrKey1 and :attrVal1 in (value(da)) ) and " +
        "(key(da) =:attrKey2 and :attrVal2 in (value(da)) )")
List<Entity> findByTwoAttrributeValues(@Param("attrKey1") String attrKey1,
                                      @Param("attrVal1") String attrVal1,
                                      @Param("attrKey2") String attrKey2,
                                      @Param("attrVal2") String attrVal2);
@Query(“从实体e中选择e加入e.dataAttributes da where”+
“(键(da)=:attrKey1和:attrVal1 in(值(da)))和”+
“(key(da)=:attrKey2和:attrVal2 in(value(da)))
列出FindBytwoAttributeValues(@Param(“attrKey1”)字符串attrKey1,
@参数(“attrVal1”)字符串attrVal1,
@参数(“attrKey2”)字符串attrKey2,
@参数(“attrVal2”)字符串attrVal2);

我是JPA的新手,任何指导都将不胜感激,因此我有以下解决方案:

@Query("select e from Entity e join e.dataAttributes da join e.dataAttributes da2 where " +
    "(key(da) =:attrKey1 and :attrVal1 in (value(da)) ) and " +
    "(key(da2) =:attrKey2 and :attrVal2 in (value(da2)) )")
List<Entity> findByTwoAttrributeValues(@Param("attrKey1") String attrKey1,
                                  @Param("attrVal1") String attrVal1,
                                  @Param("attrKey2") String attrKey2,
                                  @Param("attrVal2") String attrVal2);
@Query(“从实体e中选择e join e.dataAttributes da join e.dataAttributes da2 where”+
“(键(da)=:attrKey1和:attrVal1 in(值(da)))和”+
“(key(da2)=:attrKey2和:attrVal2 in(value(da2)))
列出FindBytwoAttributeValues(@Param(“attrKey1”)字符串attrKey1,
@参数(“attrVal1”)字符串attrVal1,
@参数(“attrKey2”)字符串attrKey2,
@参数(“attrVal2”)字符串attrVal2);

我需要用第二个标识符再次连接到dataAttributes。现在一切正常。可能不是最漂亮的。如果有更好的方法让我知道

很可能你只需要在两组键和值之间放置and
,而不是and
。使用or在语义上与and相同,对吗?在中,或将返回与OP中的第一个查询一样具体的结果,而不是使用键/值对返回更具体的结果使用“:attrVal1 in(value(da))”,您希望实现什么??毫无意义。在获取单个值时,“IN”将期望RHSda上的集合或子查询在此实例中是一个集合和一个映射。值(da)正在查询地图中的可用值否?