生成控制器和视图获取错误时:无法将int字段lms.Book.bookId设置为java.lang.Class

时间:2012-06-27 05:19:11

标签: grails grails-domain-class grails-2.0 grails-controller

为Domain类生成控制器和视图时:

class Book {

    static constraints = {
        bookId blank:false
        bookTitle blank:false
    }

    private int bookId
    private String bookTitle
    private String author
    private double price
    private Date edition
    private String publisher
}

给出错误说: 无法将int字段lms.Book.bookId设置为java.lang.Class

2 个答案:

答案 0 :(得分:1)

将“int”改为“Integer”(和“double”改为“Double”),例如

class Book {

    static constraints = {
        bookId blank:false
        bookTitle blank:false
    }

    private Integer bookId
    private String bookTitle
    private String author
    private Double price
    private Date edition
    private String publisher
}

另外,我怀疑你是否可以在整数上有一个“空白”约束,将其改为:

bookId nullable: false

假设这是您想要的(或者完全删除它,因为可以为null的虚假约束)。

答案 1 :(得分:1)

我认为如果你在字段声明中添加'private',你必须为这个字段编写getter和setter:

class Book {

    static constraints = {
        bookId blank:false
        bookTitle blank:false
    }

    private Integer bookId
...
    Integer getBookId() { this.bookId }

    void setBookId(Integer bookId) { this.bookId = bookId }
....
}