映射键值的Hibernate标准 [英] Hibernate Criteria for Map key-value

查看:125
本文介绍了映射键值的Hibernate标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的hibernate实体中有以下属性:

  @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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆