检查ElementCollection映射是否具有匹配的条目

问题描述 投票:0回答:1

我有一个@ElementCollection Map<String,String> attributes,并且如果对象在属性表中具有给定的条目,我想使用JPQL

我在Spring-Data中尝试了以下操作(全部失败)

@Query("from Organization o join o.attributes a where ?1 in entry(a)")
List<Organization> findOrganizationsWithAttribute(Map.Entry<String,String> attributeValue) ;

default List<Organization> findOrganizationsWithAttribute(String attribute, String value) {
    return findOrganizationsWithAttribute(Map.entry(attribute, value));
}

使用地图

@Query("from Organization o join o.attributes a where ?1 in entry(a)")
List<Organization> findOrganizationsWithAttribute(Map<String,String> attributeValue) ;

default List<Organization> findOrganizationsWithAttribute(String attribute, String value) {
    return findOrganizationsWithAttribute(Map.of(attribute, value));
}

如果只有一个属性,我的原始样式将起作用。

@Query("from Organization o join o.attributes a where key(a) = ?1 and value(a) = ?2")
List<Organization> findOrganizationsWithAttribute(Map<String, String> attributeValue);
java jpa spring-data-jpa spring-data jpql
1个回答
0
投票

ListSet的标准方式是MEMBER OF。我从未尝试过使用Map。但是你可以尝试

@Query("SELECT o FROM Organization o where ?1 MEMBER OF o.attributes")

作为参数值,请尝试键或地图的值。密钥可能有效。

© www.soinside.com 2019 - 2024. All rights reserved.