Grails - 创建继承者域类

时间:2014-01-28 01:43:50

标签: grails gorm

我创建了两个域类,如下所示:

组件:

class Component {
    String name;
    Integer orderOfComponent;
    Boolean limitedAccess;
    Date dateCreated;
    String alignment;
    String type;

    static belongsTo = [subpage: Subpage]

    static constraints = {
        name nullable: false, blank: false;
        orderOfComponent nullable:false, blank: false;
        limitedAccess nullable: true, blank: true;
        alignment nullable: true, blank: true, inList: ["left", "right", "center", ""];
        type nullable: false, blank: false, inList: ["text", "image", "document", "html"];
        subpage nullable: false, blank: false;
    }
}

文本:

class Text extends Component {
    String text;
    String color;
    String additionalStyle;
    String textType;
    String link;

    static constraints = {
        text nullable: true, blank: true;
        color nullable: true, blank: true;
        additionalStyle nullable: true, blank: true;
        textType nullable: true, blank: true, inList: ["p", "h1", "h2", "h3", ""];
        link nullable: true, blank: true;
    }
}

当我创建Component类的实例时,一切都很好:

new Component(name: "component" + i, orderOfComponent: i, limitedAccess: new Boolean(false), alignment: "right", type: "text", subpage: Subpage.get(1)).save(flush:true, failOnError:true);

但是当我尝试为Text类做类似的时候,我得到了错误:

new Text(name: "componentText", 
        orderOfComponent: 0, 
        limitedAccess: new Boolean(false), 
        alignment: "right", 
        type: "text", 
        subpage: Subpage.get(1), 
        text: "asdasdasdasd", 
        color: "#0F0C0A", 
        additionalStyle: "",
        textType: "p", 
        link: "http://google.pl").save(flush:true, failOnError:true);

错误:

Error 2014-01-28 02:27:10,453 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Validation Error(s) occurred during save():
- Field error in object 'adminpanel.component.Text' on field 'type': rejected value [null]; (...)

也非常相似"消息"。

有人知道为什么我不能创建Text类的实例或如何正确使用继承类的构造函数?提前谢谢。

0 个答案:

没有答案
相关问题