HQL选择嵌入式地图 [英] HQL select on embedded map
问题描述
我想在我的模型中使用(可搜索的)本地化,并且提出了以下模型:
@Entity
public class Category {
...
@ElementCollection
// key =语言代码(例如'en')和value =本地化标签
私有地图< String ,String>名称;
...
}
我想要做的不是查询对于在特定本地化中含有不敏感针头的类别(例如,在英文名称中使用'%abc%')
我试过类似于
from category where name.key =:locale和lower(name.value)like:text
但是不能解决标量集合元素异常。
现在休眠文件说,我必须使用elements()和indices()作为值和键。对于这个关键字,在索引(name)中使用 为了防止这种情况对我的模型不适用hql,我还可以如何模拟可搜索的本地化?:locale很容易,但是我怎样才能以不区分大小写的方式匹配这个语言环境的一部分值呢? p>
我终于想出了以下解决方案(其实我很确定它也可以使用plan map而不是可嵌入对象)
@Entity
public class Category {
...
@ElementCollection
@CollectionTable(name =locale_category_name,joinColumns = @ JoinColumn(name =id))
List< Localization>名称;
...
}
@Embeddable
公共类本地化{
@NotNull
public String locale;
@NotNull
public String label;
}
和查询(使用join ... with ...) p>
从类别c加入c.name l(l.locale =:locale和lower(l.label),如:text)
编辑:其实我无法让它与Map一起工作,所以我在所有的
之后使用了@Embeddable解决方案
I want to use (searchable) localization in my models, and i came up with the following model:
@Entity
public class Category {
...
@ElementCollection
//key = language code (e.g. 'en') and value = localized label
private Map<String, String> name;
...
}
What I want to do no is to query for categories which contain an caseinsensitive needle in a particular localization (e.g. with '%abc%' in their english name)
I tried something like
from Category where name.key = :locale and lower(name.value) like :text
but that fails with a cannot dereference scalar collection element exception.
Now the hibernate docu says, i have to use elements() and indices() for values and keys. For the key this would be easy using :locale in indices(name)
, but how can i then match a part of the value of this locale in an caseinsensitive manner?
And just in case this is not doable with hql with my model, how else could i model searchable localization?
I finally came up with the following solution (actually I am pretty sure it would also work using plan maps instead of the embeddable object)
@Entity
public class Category {
...
@ElementCollection
@CollectionTable(name = "locale_category_name", joinColumns=@JoinColumn(name="id"))
List<Localization> name;
...
}
@Embeddable
public class Localization {
@NotNull
public String locale;
@NotNull
public String label;
}
and the query (using join ... with ...)
from Category c join c.name l with (l.locale = :locale and lower(l.label) like :text)
Edit: Actually i was unable to get it to work with a Map, so I am using the @Embeddable solution after all
这篇关于HQL选择嵌入式地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!