hibernate - Groovy标准查询

标签 hibernate grails groovy hibernate-criteria

我有一个看起来像这样的领域模型(Domain Model)

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

或用英语“费用有一种类型,每种类型都属于一个类别”。我想编写一个条件查询,该查询将查找特定类别中的所有费用。我都尝试过
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
}

最佳答案

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

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

关于hibernate - Groovy标准查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3960441/

相关文章:

java - 无法获取隔离的 JDBC 连接 : org. hibernate.exception.JDBCConnectionException

mysql - 当表是 "product_def"时,为什么 Spring Data/Hibernate 查询表 "productDef"?

java - 从 groovy 中的嵌套映射返回一维映射

sql - Groovy,如何进行两阶段提交?在Sql.withTransaction中可以跨多个数据库管理transactionscope吗?

grails - 用户域 “enabled”字段设置为false时,使用Grails Acegi插件。用户仍然可以登录

java - 将 byte[] 与 hibernate 保存到 postgreSQL

hibernate - 来自 HQL 查询的 SQL 字符串?

image - 在grails中将图像保存到Web应用程序下的文件夹

unit-testing - 如何对具有关系映射的 grails 域类进行单元测试?

json - Grails - 如何将查询结果(来自 MongoDB)转换为没有冗余字段的 JSON 字符串