Derived Properties using aggregate functions in Grails

前端 未结 1 809
名媛妹妹
名媛妹妹 2021-01-22 19:34

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

Example below:

class Generation {

    String name

    DateTime productionStart

           


        
相关标签:
1条回答
  • 2021-01-22 20:30

    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
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题