映射键值的Hibernate标准 [英] Hibernate Criteria for Map key-value
问题描述
@ElementCollection(targetClass = String.class,fetch = FetchType。 EAGER)
@CollectionTable(name =FORMDATA,joinColumns = @JoinColumn(name =FORM_ID))
私人地图< String,String> FORMDATA;
我想用hibernate Criteria做一个查询,我想用一个给定的键来匹配表单,值对,如下所示:
criteria.add(Restrictions.like(formdata.key,%+ value + %)IGNORECASE())。
其中'key'和'value'是通过方法参数传递的。
任何人都知道这应该如何工作?对我来说,hibernate文档并不清楚。
非常感谢,
B。
因为我自己也有同样的问题,所以我做了一些试验和错误,并提出了这个解决方案:
标准attr = crit.createCriteria(formdata);
attr.add(Restrictions.and(
Restrictions.eq(indices,key),
Restrictions.eq(elements,%+ value +%)
));
索引和元素是可用于访问密钥的集合的特殊属性和映射的值,呃,地图。但显然只有在该属性的一个子标准 - 一个 crit.add(Restrictions.eq(formdata.indices,foo))
不起作用。
我还没有找到一种方法来查询映射集合的多个元素。生成的SQL始终只会生成到集合表的单个连接。
I have the following property in my hibernate entity:
@ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
@CollectionTable(name="FORMDATA", joinColumns = @JoinColumn(name="FORM_ID"))
private Map<String, String> formData;
I want to do a query with hibernate Criteria where I want to match a form with a given key-value pair, something like this:
criteria.add(Restrictions.like("formdata.key", "%"+value+"%").ignoreCase());
where 'key' and 'value' are passed via method parameters.
Anyone knows how this should work? For me the hibernate documentation is not clear on this.
Thanks a lot, B.
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.
这篇关于映射键值的Hibernate标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!