How To Solve Grails Error Repeated column in mapping for entity? on Existing Postgresql Database
Asked Answered
S

3

3

Hi i have this following Domain in Grails

class Customer {
    static mapping = {
         table 'customer'
         // version is set to false, because this isn't available by default for legacy databases
         version false
         id column:'customerid' //generator:'identity', 
    }
    Long customerid
    Long customergroupid
    String username
    String password
    String name
    String street
    String city
    String province
    String postcode
    String country
    Date dateregistered
    String balance
    Long parentid
    String parentrebate
    String metapath
    Long metalevel
    Integer smsreplystatus
    Integer status
    String reversalurl

    static constraints = {
        customerid(insert:false,update:false)
        customergroupid(max: 9999999999L)
        username(size: 1..20, blank: false)
        password(size: 1..32, blank: false)
        name(size: 1..20, blank: false)
        street(size: 0..50)
        city(size: 0..20)
        province(size: 0..20)
        postcode(size: 0..5)
        country(size: 0..20)
        dateregistered(nullable: true)
        balance()
        parentid(max: 9999999999L)
        parentrebate()
        metapath(size: 1..255, blank: false)
        metalevel(max: 9999999999L)
        smsreplystatus(max: 99999)
        status(max: 99999)
        reversalurl(size: 0..50)
    }
    String toString() {
        return "${customerid}" 
    }
}

i have current database with existing data, and has mapping to GORM with GRAG but i'm getting this error

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: postgresql.Customer column: customerid (should be mapped with insert="false" update="false")
    ... 23 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: postgresql.Customer column: customerid (should be mapped with insert="false" update="false")
Sartre answered 16/12, 2010 at 5:31 Comment(0)
H
1

Had same problem, remove "Long customerid"

This may not be a workable solution but at least the app compiles and creates the table.

Have not investigated at all as I just managed to get this working -- perhaps when setting a custom identity column, the property definition is implied (thus the "repeated column..." error)

Harlot answered 11/1, 2011 at 6:45 Comment(5)
update: Failed with property not exist error, nice. Solution, add name:'customerid' in mapping closure for custom id column. Works for me in MySQL, finally. Can preserve existing schema and give grails a test spin...Harlot
in addition to adding name attrib to mapping (and insert:false,update:false to Mapping, not constraint), you also need to add a constraint nullable:true on your custom id column; otherwise, you'll get a null property error not well documented, any of this -- basically spent a couple of hours trying out various combinations. Wasting time with a rapid development framework, ironicHarlot
insert:false,update:false not necessary just mapping { id column:'col', name:'col' } and constraints { col(nullable:true) } does the trick for me, finally!Harlot
Thanks for updating this. I encountered this problem when I used Grag tool to generate domain classes for existing mysql DB.Mohandis
@Mohandis good luck, I ditched Grails several months ago; moved on to Scala and am extremely happy to have done so. Grails was a good learning experience, but too many things went awry to justify the head banging...Harlot
S
1

Solution

If you want a different property to be the id instead of the default id property, use id name: '<your property name>' in the mapping block. In your case:

static mapping = {
    id name: 'customerid'

Example: in your code use customerDomainInstance.customerid.

If you want to map to a different column name on the table, you shouldn't declare customerid. Declare id column: '<column name>' in the mapping block and use the default id property. In your case:

static mapping = {
    id column: 'customerid'

Example: in your code use customerDomainInstance.id.

If you want to map to a different property on domain with a totally different name on the table (odd scenario), use both:

static mapping = {
    id name: 'customerid', column: 'totaly_different_column_name'

Example: in your code use customerDomainInstance.customerid.

In this case totaly_different_column_name will be name of the column on the database.


Explanation

customerid property default column name is customerid. In the mapping block you mapped Grails' default id property to the column name customerid. There's a conflict in both properties having the same column name which results in the error org.hibernate.MappingException: Repeated column in mapping for entity.


Read more on Grails documentation.

Sundae answered 25/8, 2022 at 12:12 Comment(1)
id name: 'customerid' was exactly what I was looking for - thanks!Acyl
P
0

Just to put a bow on this 11 years later...

Will compile if you put

customerid insertable:false updateable:false

under mapping instead of listing it under constraint; you'll also need to remove what you have for 'id' in the mapping. You're getting this error because you're specifying a second variable 'id' that maps to customer_id column.

Pisces answered 4/2, 2022 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.