0

While generating controller and view for a Domain class as:

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
}

Giving Error saying : Can not set int field lms.Book.bookId to java.lang.Class

tim_yates
  • 167,322
  • 27
  • 342
  • 338
AAA
  • 348
  • 1
  • 4
  • 19

2 Answers2

1

Change "int" to "Integer" (and "double" to "Double" too), e.g.

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
}

Also, I doubt whether you can have a "blank" constraint on an Integer, change it to:

bookId nullable: false

assuming that is what you want (or remove it altogether, as the nullable: false constraint is implicit).

Jon Burgess
  • 2,035
  • 2
  • 17
  • 27
1

I think if u add 'private' to field declaration, u have to write getter and setter for this field:

class Book {

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

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

    void setBookId(Integer bookId) { this.bookId = bookId }
....
}
jenk
  • 1,043
  • 7
  • 8
  • This answer is correct. You must declare them public or provide the getter/setters yourself. Don't know why this answer isn't marked? – Bart May 12 '13 at 09:31