【发布时间】:2015-12-23 21:23:01
【问题描述】:
我在一个 groovy 文件中有一个抽象类:
实施 1
public abstract class Item {
public String testStr;
public String getBigTestStr(){
String s = "___" + this.testStr;
return s;
}
}
继承自
class Material extends Item {
public String testStr;
static marshalling = {
detail {
includes "bigTestStr"
}
summary {
includes "bigTestStr"
}
}
static mapping = {
table 'materialset'
id column: 'NODEID'
testStr column: 'MATERIALTYPE'
version false
}
}
这个想法是点击材质的端点将返回Item.bigTestStr()的返回值。但是,当我通过Item.bigTestStr() 进行跟踪时,调试的变量表显示this.testStr 的值,但当它添加到s 时为空。见这里:
我尝试从Material 中取出testStr 属性
实施 2
class Material extends Item {
static marshalling = {
detail {
includes "bigTestStr"
}
summary {
includes "bigTestStr"
}
}
static mapping = {
table 'materialset'
id column: 'NODEID'
testStr column: 'MATERIALTYPE'
version false
}
}
但我仍然遇到同样的问题。
对于这两种实现,端点都返回
{
bigTestStr: ____null
}
如何获取Material.testStr 的实际值以供其父类中的函数使用?
更新
正如 Emmanuel 指出的,实现 2 是使用父类属性的正确方法。但是,此实现似乎不适用于将父类的属性映射到数据库列。所以真正的问题是:如何让Material.testStr 映射到数据库列?
【问题讨论】:
-
顺便说一下 Grails 版本 2.3.8。
标签: grails grails-orm grails-2.0 grails-domain-class