【问题标题】:Derived Properties using aggregate functions in Grails在 Grails 中使用聚合函数的派生属性
【发布时间】:2025-12-07 08:05:02
【问题描述】:

我正在尝试基于包含的对象创建派生属性。

下面的例子:

class Generation {

    String name

    DateTime productionStart

    DateTime productionEnd

    static belongsTo = [line: Line]

    static hasMany = [bodyStyles: BodyStyle, engines: Engine, models: Model]

    static constraints = {
        line nullable: false
        name nullable: false, unique: ['line'], maxSize: 255, blank: false
    }

    static mapping = {
        // I've tried but this solution causes errors
        productionStart formula: 'MIN(engines.productionStart)'
        // I've tried but this solution causes errors
        productionEnd formula: 'MAX(engines.productionEnd)'
    }
}

class Engine {

    String name

    Integer horsePower

    DateTime productionStart

    DateTime productionEnd

    static belongsTo = [generation: Generation]

    static hasMany = [models: Model]

    static constraints = {
        generation nullable: false
        name nullable: false, unique: ['generation', 'horsePower'], maxSize: 255, blank: false
        horsePower nullable: false
        productionStart nullable: false
        productionEnd nullable: true
    }

    static mapping = {
        productionStart type: PersistentDateTime
        productionEnd type: PersistentDateTime
   }
}

我已阅读Derived Properties Documentation,但我的情况比与复杂对象无关的公式要复杂一些。

你可以在上面的代码中找到的解决方案导致错误::

由 GrailsTagException 引起:执行标签时出错:在第 [23] 行评估表达式 [Generation.findAll()] 时出错:无法执行查询; SQL [select this_.id as id22_0_, this_.version as version22_0_, this_.line_id as line3_22_0_, this_.name as name22_0_, MAX(engines.productionEnd) as formula0_0_, MIN(engines.productionStart) as formula1_0_ from generation this_];嵌套异常是 org.hibernate.exception.SQLGrammarException: could not execute query

【问题讨论】:

    标签: grails groovy grails-orm


    【解决方案1】:

    另一种尝试方法是创建一个getter而不是派生属性:

    class Generation {
    
        String name
    
        DateTime productionStart
    
        DateTime productionEnd
    
        static transients = ['productionStart','productionEnd']
    
        static belongsTo = [line: Line]
    
        static hasMany = [bodyStyles: BodyStyle, engines: Engine, models: Model]
    
        static constraints = {
            line nullable: false
            name nullable: false, unique: ['line'], maxSize: 255, blank: false
        }
    
    
        DateTime getProductionStart() {
          def datetime = Engine.createCriteria().get {
            eq('generation',this)
            projections {
              min('productionStart')
            }
          }
    
          return datetime
    
        }
    
        DateTime getProductionEnd() {
          def datetime = Engine.createCriteria().get {
            eq('generation',this)
            projections {
              max('productionEnd')
            }
          }
    
          return datetime
        }
    
    }
    

    【讨论】: