这就是我想要做的:
class Test {
String name
static mapping = {
name defaultValue: "test_${Test.count()}"
}
}
因此,当创建一个新的“测试”对象时,其名称为test_1,test_2,test_3等,具体取决于已经存在多少个测试对象。上面的方法不起作用,因为“test.count在Grails应用程序外部使用了”
最佳答案
您可以初始化属性,而不是通过映射闭包指定值。
class Test {
String name = "test_${Test.count()}"
}
要么
class Test {
String name = initName()
private static String initName() {
def count = Test.count()
return "test_" + count
}
}
关于grails - Grails域对象成员如何具有基于计算值的默认名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25473025/