带有hasmany String的Grails/GORM标准查询 [英] Grails / GORM criteria query with hasmany String

查看:24
本文介绍了带有hasmany String的Grails/GORM标准查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的域对象 (Cat):

I have a domain object (Cat) like this:

class Cat {
   String name

   static hasMany = [
      nicknames: String
   ]
}

(猫有名字,也有很多昵称(都是字符串))

(A cat has a name, and also has many nicknames (which are Strings))

我正在尝试查询所有具有特定昵称的猫.

And I am trying to query all the cats with certain nicknames.

我已经试过了:

PagedResultList getCatsByNickname(String nickname, Map params) {
   PagedResultList results = Cat.createCriteria().list(params) {
      'ilike'('nicknames','%'+nickname+'%')
   }
   return results
}

但它从不返回任何结果.(如果我将查询更改为仅使用简单的名称属性,则可以找到具有该名称的所有猫,但我想查询昵称.)

But it never returns any results. (If I change the query to just use the simple name attribute, it works finding all cats with that name, but I want to query against the nicknames.)

我也试过这个:

PagedResultList getCatsByNickname(String nickname, Map params) {
   PagedResultList results = Cat.createCriteria().list(params) {
      'nicknames' {
         'ilike'('nicknames','%'+nickname+'%')
       }
   }
   return results
}

但我收到错误:org.hibernate.MappingException:集合不是关联:example.Cat.nicknames

But I get the error: org.hibernate.MappingException: collection was not an association: example.Cat.nicknames

那么问题是,我如何查询一个字符串类型的 hasMany?

So the question is, how do I query against a hasMany of type String?

推荐答案

经过大量的尝试和研究,我发现这适用于 Grails 2.4.0,我不知道旧版本.

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%'
}

诀窍是使用'n.元素'

这篇关于带有hasmany String的Grails/GORM标准查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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