派生属性在Grails中使用集合函数 [英] Derived Properties using aggregate functions in Grails

查看:102
本文介绍了派生属性在Grails中使用集合函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据包含的对象创建派生属性。



下面的例子:

  class Generation {

字符串名称

DateTime productionStart

DateTime productionEnd

static belongsTo = [line:Line]

static hasMany = [bodyStyles:BodyStyle,engines:Engine,models:Model]
$ b static constraints = {
line nullable: false
name nullable:false,unique:['line'],maxSize:255,blank:false
}

static mapping = {
// I'已经尝试过,但这种解决方案导致错误
productionStart公式:'MIN(engines.productionstart)'
//我试过了,但是这个解决方案导致错误
productionEnd公式:'MAX(engines.productionEnd )'
}
}

类引擎{

字符串名称

整数horsePower

DateTime productionStar t

DateTime productionEnd

static belongsTo = [generation:Generation]

static hasMany = [models:Model]

静态约束= {
生成可空:false
名称可空:false,唯一:['generation','horsePower'],maxSize:255,空白:false
horsePower可空:false
productionStart可为空:false
productionEnd可为空:true
}

静态映射= {
productionStart类型:PersistentDateTime
productionEnd类型:PersistentDateTime
}
}

我试过了派生属性文档,但我的情况比复杂对象不复杂的公式稍微复杂一些。



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


由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:无法执行查询


解决方案

尝试它是创建一个getter而不是派生属性:

  class Generation {

字符串名称

DateTime productionStart

DateTime productionEnd

static transients = ['productionStart','productionEnd']

static belongsTo = [line:行]

static hasMany = [bodyStyles:BodyStyle,引擎: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')
}
}

返回日期时间

}

DateTime getProductionEnd(){
def datetime = Engine.createCriteria()。get {
eq('generation',this)
预测{
max('productionEnd')
}
}

return datetime
}

}


I'm trying to create derived properties based on contained objects.

Example below:

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

I've readed Derived Properties Documentation but my case is a little bit more complicated than formulas not associated with complex objects.

The solution that you can find in the code above results in an error::

Caused by GrailsTagException: Error executing tag : Error evaluating expression [Generation.findAll()] on line [23]: could not execute query; 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_]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

解决方案

Another way to try it is to create a getter instead of derived properties:

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
    }

}

这篇关于派生属性在Grails中使用集合函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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