2

假设我有以下课程:

abstract class Fruit {
    String name

    static mapping = {
        discriminator column: 'type'
    }

class Banana extends Fruit {
    static mapping = {
        discriminator: 'Banana'
    }
}

class Apple extends Fruit {
    static mapping = {
        discriminator: 'Apple'
    }
}

我需要实现一个搜索算法,这样,给定一个 JSON 字符串,我可以在数据库中找到一个特定的 Fruit 实例。例如:

{
    "type": "Apple"
    "name": "Green"
}

或者

{
    "type": "Banana"
    "name": "Green"
}

问题是,水果可以有相同的名称,所以如果我只是搜索这个:

Fruit.getByName('Green')

它可能会返回AppleBanana。我需要能够根据类型过滤它的类型,比如:

Fruit.getByNameAndType('Green', 'Apple')

我如何在 Grails 中做到这一点?

4

3 回答 3

2

查看生成的数据库。

class您可以在 Fruit 类的条件搜索中使用一列。

也许这可以作为

   Fruit.findAllByColorAndClassLike('Green','%Apple')

于 2012-12-06T06:33:33.603 回答
0

Should this meet your need?

def fruits = Fruit.findAll("from Fruit where type = :type and name = :name", [type: "Apple", name: "Green"])
于 2012-12-06T09:33:21.360 回答
0
Apple.getByName('Green')

应该管用。你试过吗?

于 2012-12-06T03:04:17.607 回答