grantmcconnaughey
grantmcconnaughey

Reputation: 10719

Grails "duplicate field name" error in static mapping closure

I am receiving the following errors in my "Class" class in Grails. For each field, it is telling me that I have a duplicate field. Which doesn't make any sense, because all I'm trying to do is map the fields with their associated table columns. The class fields and the fields in my mapping closure are all underlined. Here is my class so far:

package booklist

class Class {

Integer id
String name
String description
String instructor
String courseNumber
String lineNumber
List books
BigDecimal bookTotalPrice
String sequenceNumber
String subjectCode


static constraints = {

}

static mapping = {
    //Uses the default datasource
    table ''

    columns {
        id column: 'class_id'
        name column: 'class_name'
        description column: 'course_description'
        instructor column: 'instructor_name'
        courseNumber column: 'course_number'
        lineNumber column: 'line_number'
        bookTotalPrice column: 'book_total_price'
        sequenceNumber column: 'sequence_number'
        subjectCode column: 'subject_code'
    }

    }
}

Upvotes: 0

Views: 504

Answers (1)

lucke84
lucke84

Reputation: 4636

You don't need to declared in static mapping the fields that you don't need to rename. Just write this:

package booklist

class MyClass {

    Integer id
    String name
    String description
    String instructor
    String courseNumber
    String lineNumber
    List books
    BigDecimal bookTotalPrice
    String sequenceNumber
    String subjectCode

    static mapping = {
        description column: 'course_description'
    }
}

Grails works with the CoC (Convention Over Configuration) approach: if you don't need to change something, don't write it and a convention will be used.

For more details about the column mapping, take a look at the Grails documentation: http://grails.org/doc/latest/ref/Database%20Mapping/column.html

Upvotes: 2

Related Questions