2

含まれているオブジェクトに基づいて派生プロパティを作成しようとしています。

以下の例:

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 [this_.id を id22_0_、this_.version を version22_0_、this_.line_id を line3_22_0_、this_.name を name22_0_、MAX(engines.productionEnd) を formula0_0_、MIN(engines.productionStart) を formula1_0_ を生成 this_ から選択]; ネストされた例外は org.hibernate.exception.SQLGrammarException: クエリを実行できませんでした

4

1 に答える 1

3

それを試す別の方法は、派生プロパティの代わりに 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
    }

}
于 2013-10-03T17:13:23.923 に答える