您的位置:首页 > 大数据 > 人工智能

Grails指南摘要-305-扩展和继承

2013-06-04 22:12 218 查看
多个domain继承自同一个对象

class Person {
String frstName
String lastName
Integer age
}
class Employee extends Person {
String employeeNumber
String companyName
}
class Player extends Person {
String teamName
}


生成的关系为一张表,可以在表中增加一个鉴别字段区分哪条记录是employee,哪条是player,这种方式称为鉴别器识别

class Employee extends Person {
String employeeNumber
String companyName
static mapping = {
// the value of the discriminator column for
//Employee instances should be 'working people'
discriminator 'working people'
}
}


还可以用整数来识别,用关系方式设计表会常用这种方式,鉴别器中42代表employee

class Employee extends Person {
String employeeNumber
String companyName
static mapping = {
discriminator value: '42', type: 'integer'
}
}


用对象的方式更加直观,把继承关系设置成多个one-to-one对象,分别放在不同的关系表中存放

class Person {
String frstName
String lastName
Integer age
static mapping = {
tablePerHierarchy false
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: