0
votes

I'm trying to map a ManyToOne relationship with a composite key.

I'd like to be able to access the OnesolPeNames domain object from RecoverySetup like so recoverySetup.onesolPeNames.peNameU

I've added the following code to my RecoverySetup mapping.

        oneSolution {
                column name: 'division' 
                column name: 'peid'
            };

Table A

class RecoverySetup implements Serializable {

    static constraints = {

    }

    static mapping = {
        table "recovery_setup"

        id composite: ["division", "peid", "orgkey"]

        columns{
            division column: 'division'
            peid column: 'peid'
            orgkey column: 'org_key'

            oneSolution {
                    column name: 'division' 
                    column name: 'peid'
                };
        }
    }

    String division
    String peid
    String orgkey
    OnesolPeNames oneSolution

}

Table B

class OnesolPeNames implements Serializable {

    static constraints = {

    }

    static mapping = {
        table "ONESOL_pe_names"

        id composite: ["division", "peid"]

        columns{
            division column: 'division', length: 8, sqlType: "char"
            peid column: 'pe_id', length: 12, sqlType: "char"
            peNameU column: 'pe_name_u', length: 50, sqlType: "char"
        }
    }

    static hasMany = [recoverySetups: RecoverySetup]

    String division
    String peid
    String peNameU

}

I get the following exception

Caused by MappingException: Repeated column in mapping for entity: org.hri.pisr.domain.RecoverySetup column: division (should be mapped with insert="false" update="false")

I also found this SO post One-to-Many With Composite Keys and Different Column Names in Grails

1
At risk of asking the obvious, has you tried to include "insert: false, update: false" in the mappings?christopher
I've tried it like so oneSolution { column name: 'division', insert: false, update: false column name: 'peid', insert: false, update: false } with the same errorCode Junkie
What about in the column definition?christopher
I'm new to gorm, please explain.Code Junkie
Nevermind! It was a dumb suggestion. Do you need to have a composite key? And you can make the relationship bidirectional with static hasOne = [OnesolPeNames: 'oneSolution']christopher

1 Answers

0
votes

Resolved by GUL Foreign key (FK_ must have same number of columns as the referenced primary key

class RecoverySetup implements Serializable {

static mapping = {
    table "recovery_setup"
    id composite: ["division", "peid", "orgkey"]
    columns {
        orgkey column: 'org_key', length: 8, sqlType: "char"

        oneSolName {
            column name: 'division'
            column name: 'peid'
        }
    }
    oneSolName updateable: false, insertable: false
}

static belongsTo = [oneSolName: OnesolPeNames]
...

}