是否可能有一个域对象具有引用另一个对象上的非主键的字段(我正在处理遗留数据库)。
我拥有的是flea_name,而不是flea_id (它是主键)。是否有一种方法可以让GORM根据非主唯一的键(如名称、代码等),而不是它的id,自动地从跳蚤表中提取正确的对象?例如:
class Flea {
def id //This is the primary key in the table
def name //This is a unique key
}
class Puppy {
def flea //This is stored as the flea's unique, but non-primary,
// name in the puppy table
//The puppy table has a flea_name, not a flea_id, as would be expected
static mapping = {
flea column: 'flea_name'//This will point to the flea's name,
//but I want to hydrate it to be a Flea
}
}提前感谢
发布于 2011-02-02 07:12:11
简短的回答:你不是这样做的。
让grails使用id链接数据库中的对象。然后,如果您需要访问跳蚤名称,可以重写它的toString()方法来返回跳蚤名称。或者像控制器/服务或gsps中的任何其他属性一样访问该属性。
发布于 2011-02-01 22:01:02
你能详细解释一下你为什么要这么做吗?如果对象关系真的是一个小狗有一个跳蚤,那么你真的想通过Id链接正常。
然而,如果你真正的意思是一个小狗有很多跳蚤,那么它要么有一个单独的跳蚤集合,或者更有可能在这个例子中,你并不是真的想在这里建立关系,而是小狗有一个数字字段来识别跳蚤的数量。
这听起来怎么样?
发布于 2011-02-02 16:37:46
您可能想看看自定义ORM映射
举个例子:
class Flea {
def id
def name
}
class Puppy {
static hasMany = [fleas:Flea]
static mapping = {
table 'puppy'
name column:'puppy_name'
fleas column:'flea_name'
}
}对不起,因为链接中有一些空格,它是:http://grails.org/doc/1.0.x/guide/single.html#5.5.2自定义ORM映射
1:http://grails.org/doc/1.0.x/guide/single.html#5.5.2自定义ORM映射
https://stackoverflow.com/questions/4867857
复制相似问题