Java 检查ElementCollection映射是否具有匹配的条目
我有一个Java 检查ElementCollection映射是否具有匹配的条目,java,jpa,spring-data-jpa,spring-data,jpql,Java,Jpa,Spring Data Jpa,Spring Data,Jpql,我有一个 @ElementCollection @CollectionTable( name = "orgattributes", joinColumns = @JoinColumn(name = "org_id")) @MapKeyColumn(name = "attribute") @Column(name = "value") Map<String,String> attributes 使用列表或集合执行此操作的标准方法是成员。我从未用Map尝试过。但是你可以试试 @Qu
@ElementCollection
@CollectionTable(
name = "orgattributes", joinColumns = @JoinColumn(name = "org_id"))
@MapKeyColumn(name = "attribute")
@Column(name = "value")
Map<String,String> attributes
使用
列表
或集合
执行此操作的标准方法是成员
。我从未用Map
尝试过。但是你可以试试
@Query(“从组织o中选择o,其中?o.attributes的1个成员”)
作为参数值,请尝试键或映射的值。键可能会起作用。不过我需要它同时包含键和值。
@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);
select * from organization o where o.orgid in (select orgid from orgattribute a where a.attribute = ?1 and a.value = ?2)