【问题标题】:Grails: Transients property in Constraints.groovyGrails:Constraints.groovy 中的瞬态属性
【发布时间】:2014-06-26 03:30:22
【问题描述】:

我想用休眠类(速率)替换 grails 中的域类。因此,可以通过在 src/java 中创建文件 RateConstraints.groovy 来添加对休眠类的约束,它可以很好地用于验证休眠类。现在我需要使用 RateConstraints.groovy 将瞬态属性添加到休眠类。

例如:

考虑一个java类

class Rate {        
        Long id 
        String code         
    }

RateConstraints.groovy ../src/java 中的文件

constraints = { 
    id ( nullable:true )
    code( nullable: false )
}

如何在RateConstraints.groovy 文件中添加瞬态属性

transients = ['startDate', 'endDate']

【问题讨论】:

    标签: grails grails-domain-class


    【解决方案1】:

    找到答案

    RateConstraints.groovy ../src/java 中的文件

    瞬态 = ['startDate', 'endDate']

    Rate.metaClass.getStartDate << {-> startDate  }
    Rate.metaClass.setStartDate << {it -> startDate = it }
    Rate.metaClass.getEndDate << {-> endDate  }
    Rate.metaClass.setEndDate << {it -> endDate = it }
    
    constraints = { 
        id ( nullable:true )
        code( nullable: false )
    }
    

    有效

    【讨论】:

    • 这毫无意义。你把代码放在哪里了?
    【解决方案2】:

    你没有。如果您想将休眠 POJO 中的属性标记为瞬态,那么您可以在 POJO 本身中使用常规的 @Transient 注释(或者如果您使用的是 hbm.xml,则根本不映射它)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2012-02-25
      • 2011-07-20
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      相关资源
      最近更新 更多