Grails / GORM criteria query with hasmany String

前端 未结 3 862
天命终不由人
天命终不由人 2021-01-11 19:25

I have a domain object (Cat) like this:

class Cat {
   String name

   static hasMany = [
      nicknames: String
   ]
}

(A cat has a name,

相关标签:
3条回答
  • 2021-01-11 19:40

    You can also use HQL (tested with Grails 2.5.0):

    Cat.findAll("from Cat c inner join c.nicknames as n where upper(n) like '%'||?||'%'", [nickname.toUpperCase()])
    
    0 讨论(0)
  • 2021-01-11 19:49

    You can use HQL for querying in such a scenario. For example,

    Cat.findAll("from Cat c where :nicknames in elements(c.nicknames)", [nicknames:'kitty'])
    
    0 讨论(0)
  • 2021-01-11 19:55

    After a lot of trying and researching, I found this will work with Grails 2.4.0, I don't know about older versions.

    Cat.withCriteria {
        createAlias('nicknames', 'n') 
        ilike 'n.elements', '%kitty%'
    }
    

    The trick is to use 'n.elements'

    0 讨论(0)
提交回复
热议问题