Using HQL to query on a Map's Values

爱⌒轻易说出口 提交于 2019-12-07 03:16:27

You should use elements(). I tried simulating your example with the following class

@Entity
@Table(name="Dummy")
public class TestClass {
    private Integer id;
    private Map<String, String> myMap;

    @Id
    @Column(name="DummyId")
    @GeneratedValue(generator="native")
    @GenericGenerator(name="native", strategy = "native")
    public Integer getId() {
        return id;
    }

    @ElementCollection
    public Map<String, String> getMyMap() {
        return myMap;
    }


    public void setId(Integer id) {
        this.id = id;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }
}

And persisted a few instances, which constain maps of a similar structure to what you have in your example. (using < String, String > since the values in your example are strings)

The following query gives me all instances of TestClass, where the map contains a specific value

SELECT DISTINCT myClass 
FROM TestClass myClass JOIN myClass.myMap myMap 
WHERE ? in elements(myMap)

In my particular case, I ended up having to use an SQL query. Not ideal, but it worked.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!