0

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

Community
  • 1
  • 1
Code Junkie
  • 7,602
  • 26
  • 79
  • 141
  • At risk of asking the obvious, has you tried to include "insert: false, update: false" in the mappings? – christopher Jun 30 '15 at 19:55
  • I've tried it like so oneSolution { column name: 'division', insert: false, update: false column name: 'peid', insert: false, update: false } with the same error – Code Junkie Jun 30 '15 at 20:03
  • 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 Jun 30 '15 at 20:17
  • Unfortunately this is a legacy database and I need to pull back a OnesolPeNames record with the recovery setup. So no way to do it without the composite key. – Code Junkie Jun 30 '15 at 20:19
  • hasOne gives me the following error org.hibernate.HibernateException: Missing column: onesol_pe_names_division in TEST_pi_salary_recovery.dbo.recovery_setup – Code Junkie Jun 30 '15 at 20:27
  • Yeah, it's because it can't update the database. I wasn't aware it was a legacy DB. Grails and GORM tends to work well when you are implementing a DB from scratch. Okay, we can move this to chat! – christopher Jun 30 '15 at 20:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82018/discussion-between-christopher-and-code-junkie). – christopher Jun 30 '15 at 20:35
  • Does this answer your question? [Foreign key (FK\_ must have same number of columns as the referenced primary key](https://stackoverflow.com/questions/31167194/foreign-key-fk-must-have-same-number-of-columns-as-the-referenced-primary-key) – Maicon Mauricio Aug 26 '22 at 12:48

1 Answers1

0

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]
...

}

Community
  • 1
  • 1
Code Junkie
  • 7,602
  • 26
  • 79
  • 141