Grails可以浮动属性吗?

时间:2014-06-13 01:03:49

标签: grails

我有一个具有float属性的类,如果我设置为nullable 如果它不会保存并给出验证错误。我可以不设置可空 数字?它似乎适用于Strings。

1 个答案:

答案 0 :(得分:2)

是的,你可以拥有可空的Float。

// grails-app/domain/com/demo/Product.groovy
package com.demo

class Product {
    Float nullableFloat
    Float notNullableFloat
    static constraints = {
        nullableFloat nullable: true
        notNullableFloat nullable: false
    }
}

以下grails shell会话显示与之交互并证明nullableFloat实际上是可空的:

groovy:000> import com.demo.Product
===> [import com.demo.Product]
groovy:000> new Product().validate()
===> false
groovy:000> new Product(notNullableFloat: 21.12).validate()
===> true
groovy:000> new Product(nullableFloat: 21.12, notNullableFloat: 21.12).validate()
===> true

我希望有所帮助。