我有一个
@ElementCollection
@CollectionTable(
name = "orgattributes", joinColumns = @JoinColumn(name = "org_id"))
@MapKeyColumn(name = "attribute")
@Column(name = "value")
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));
}
使用 map
@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);
我想做的 SQL 等效项是
select * from organization o where o.orgid in (select orgid from orgattribute a where a.attribute = ?1 and a.value = ?2)
最佳答案
使用List
或Set
执行此操作的标准方法是MEMBER OF
。我从未尝试过使用Map
。但你可以尝试一下
@Query("SELECT o FROM Organization o where ?1 MEMBER OF o.attributes")
尝试使用 map 的键或值作为参数值。 key 可能有效。
关于java - 检查 ElementCollection 映射是否有匹配的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59584088/