NEO

蜀道难,难于上青天!

导航

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

Posted on 2013-06-04 22:12  页面载入出错  阅读(192)  评论(0编辑  收藏  举报

多个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
    }
}