0

我有一个看起来像这样的域模型

Category 1 ------- * Type 1 ------- * Expense

或者用英文“一个费用有一个类型,每个类型都属于一个类别”。我想编写一个 Criteria 查询来查找特定类别中的所有费用。我试过这两个

Expense.withCriteria {
    eq('type.category', someCategoryInstance)
}

和这个

Expense.withCriteria {
    type {
        eq('category', someCategoryInstance)
    }    
}

但是它们都不起作用,我错过了什么?

更新

我被要求显示域类,所以它们是:

public class Category {

    String description
    static hasMany = [types: Type]  
}

public class Type {

    String description
    static hasMany = [expenses: Expense]
    static belongsTo = [category: Category]
}

public class Expense {

    static belongsTo = [type: Type]

    Date date
    String description
    float amount
}
4

1 回答 1

1

根据声明关联的方式,类型表可能不会加入到查询中。您可以明确告诉它加入join您的条件中的 a 。

Expense.withCriteria {
    join('type')
    type {
        eq('category', someCategoryInstance)
    }    
}
于 2010-10-18T16:14:19.223 回答