简体   繁体   English

搜索属性字符串类型,一些带有hql的字符用于休眠

[英]Search into a property String type, some characters with hql for hibernate

I don't know if I can do this. 我不知道我能不能做到这一点。 I have an attribute where I store with hibernate an String: String tags this property could value for example: tags="car,airplane,bycicle" 我有一个属性,我在其中存储一个休眠的String:一个String tags此属性可以赋值,例如: tags="car,airplane,bycicle"

Now I would like get the instances which its tags value contain the substring "car": 现在,我想获取其标签值包含子字符串“ car”的实例:

Object A --> tags="car,airplane"
Object B --> tags="bycicle"
Object C --> tags="bycicle,car"

Return A and C 返回A和C

There is any way to do this? 有什么办法吗?

I try to classify the object by tags and then restore objects which contains one or more specific tags. 我尝试通过标签对对象进行分类,然后还原包含一个或多个特定标签的对象。

The idea is to do same that Stackoverflow does with tags field when you go to ask a question. 这样做的想法与在您提出问题时Stackoverflow对标签字段所做的相同。

This is exactly why database normalization is important. 这就是为什么数据库规范化很重要的原因。 Multiple values should never be stored in a single column. 多个值永远不能存储在单个列中。 You should have a Tag entity, and have a ManyToMany association between your entity and the Tag entity. 您应该有一个Tag实体,并且在您的实体和Tag实体之间具有ManyToMany关联。 If you had that, you could simply do 如果有的话,您可以做

select p from Post p inner join p.tags tag where tag.name = 'car'

With your current design, your best bet is to do 使用当前的设计,最好的选择是

select p from Post p where p.tags like '%car%'

but this will be much slower, and will return posts having the tags careful or vacarm . 但这会慢很多,并且会返回贴有细心vacarm标签的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM