Hibernate Criteria for Map key-value

自闭症网瘾萝莉.ら 提交于 2019-12-05 20:30:50

Since I had the same question myself, I did some trial and error and came up with this solution:

Criteria attr = crit.createCriteria("formdata");
attr.add(Restrictions.and(
            Restrictions.eq("indices", key), 
            Restrictions.eq("elements", "%" + value + "%")
));

The "indices" and "elements" are special properties of collections that can be used to access the key and value of the mapped, uh, Map. But apparently only in a sub-criterion on that property -- a crit.add(Restrictions.eq("formdata.indices", "foo")) does not work.

I also haven't found a way to query on multiple elements of the mapped collection. The generated SQL always only generates a single join to the collection table.

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