Search code examples
grailsgrails-orm

Grails GORM binding field twice


I have a simple domain object.

class Product {

    public static final String TRACE_SKU="236"

    Integer xRefId

    static constraints = {
        xRefId(nullable:true)
    }

    static mapping = {
        table 'product'
        version false
        id generator:'identity', column:'id'
        xRefId column:'xref_id'
        cache usage: 'nonstrict-read-write'
   }

}

I receive the following error on application startup as well as when running test cases.

Repeated column in mapping for entity: com.appdroplet.tricor.common.domain.product.Product column: xref_id (should be mapped with insert="false" update="false")

In addition when I examine the error logs I see this message.

[ 15.11.16 09:34:50.403] [main] [DEBUG] [org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder] [GrailsDomainBinder] Mapping Grails domain class: com.appdroplet.tricor.common.domain.product.Product -> product
[ 15.11.16 09:34:50.403] [main] [DEBUG] [org.codehaus.groovy.grails.commons.spring.ReloadAwareAutowireCapableBeanFactory] Returning cached instance of singleton bean 'orgGrailsBeansConstraintsEvaluator'
[ 15.11.16 09:34:50.405] [main] [DEBUG] [org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder] [GrailsDomainBinder] bound property [id] to column name [id] in table [product]
[ 15.11.16 09:34:50.405] [main] [DEBUG] [org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder] [GrailsDomainBinder] Binding persistent property [XRefId]
[ 15.11.16 09:34:50.405] [main] [DEBUG] [org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder] [GrailsDomainBinder] Binding property [XRefId] as SimpleValue
[ 15.11.16 09:34:50.407] [main] [DEBUG] [org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder] [GrailsDomainBinder] bound property [XRefId] to column name [xref_id] in table [product]
[ 15.11.16 09:34:50.407] [main] [DEBUG] [org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder] [GrailsDomainBinder] Binding persistent property [xRefId]
[ 15.11.16 09:34:50.407] [main] [DEBUG] [org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder] [GrailsDomainBinder] Binding property [xRefId] as SimpleValue
[ 15.11.16 09:34:50.407] [main] [DEBUG] [org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder] [GrailsDomainBinder] bound property [xRefId] to column name [xref_id] in table [product]

For some reason Grails is binding the same property twice.

Project is using Grails 2.3.0


Solution

  • You can not have a property ended by Id.

    Having the following class

    Book {
        Author author
    }
    

    book.authorId by grails convention means that you want to get the Id of the property author. Other use as you did, could produce an error.

    The author in the book table is mapped as author_id by default.

    You could learn more about GORM in its documentation