简体   繁体   中英

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"

Now I would like get the instances which its tags value contain the substring "car":

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

Return A and 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.

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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