0

I'm working with a legacy database where Table A consist of 3 composite keys and Table B consist of 2 composite keys which are the same as two of the composite keys from Table A

OnesolPeNames

class OnesolPeNames implements Serializable {

    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]

    ....

RecoverySetup

class RecoverySetup implements Serializable {

    static mapping = {
        table "recovery_setup"

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

        columns {
            division column: 'division', length: 10, sqlType: "char"
            peid column: 'peid', length: 12, sqlType: "char"
            orgkey column: 'org_key', length: 8, sqlType: "char"

            oneSolName column: ['division', 'peid']


        }
    }

    static belongsTo = [oneSolName: OnesolPeNames]

    ....

I'm trying to access OnesolPeNames like so.

recoverySetup.onesolPeNames.peNameU.

I'm getting the following error

Caused by MappingException: Foreign key (FK_ib9w9pn893cwi1dkk84qs31bx:recovery_setup [division, peid,onesol_pe_names_division,onesol_pe_names_peid])) must have same number of columns as the referenced primary key (ONESOL_pe_names [division,pe_id])

I'm setting oneSolName column to ['division', 'peid'], where is onesol_pe_names_division,onesol_pe_names_peid coming from?

Could it be that my composite keys are a part of PK but also plays role as a FK which is known as "Derived Identities"?

Code Junkie
  • 7,602
  • 26
  • 79
  • 141
  • I see that you're having problems to map your legacy database. I have a Grails Application that maps a legacy database where all tables have at least two composite keys. The difference is that I'm not doing the map using DomainClass, I'm using hbm.xml files (the Hibernate traditional way). – cantoni Jul 01 '15 at 17:08
  • Same situation here, every one of my tables are two or more composite keys. Is there away to do this using a DomainClass? Management would prefer that approach if possible. – Code Junkie Jul 01 '15 at 17:11
  • I don't remember the reason now, but I had problems to map my tables using DomainClasses. When I decided to do the mapping using hbm.xml files everything started to working fine. To generate the hbm.xml files I used a plugin for Eclipse called HibernateTools. This plugin connects to a legacy database (via JDBC) and automatically generates the hbm.xml and the .java related. – cantoni Jul 01 '15 at 17:21
  • I'm thinking my issue might be Derived Identities – Code Junkie Jul 01 '15 at 17:22

1 Answers1

3

I had a similar issue and I resolved mapping table in this way:

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]
    ...
}
GUL
  • 1,175
  • 1
  • 13
  • 22
  • Worked perfectly, what does it mean when we say oneSolName isn't updatable or instertable? Does this mean I wouldn't be able to update any records in that table? – Code Junkie Jul 01 '15 at 17:35
  • 1
    You can insert/update records in OnesolPeNames class as you want. But when you set the oneSolName value in class RecoverySetup you will do that setting/updating the primary key – GUL Jul 01 '15 at 17:44