AKSM
AKSM

Reputation: 327

Grails GORM association for different FK in a composite class

We have two domain classes like below

 package com.x.y.z;
 Class A {
    String name
    String status
    String specialID


 static mapping = {
    id type:Long, sqlType:'INT'
 }

 }

  package com.x.y.z;
  Class B {
     A a
     String name

  }

Now when we bring up our server and data from the table starts being indexed in Elastic on bulkload we get error

Caused by ObjectNotFoundException: No row with the given identifier exists: [com.x.y.z.A#300]

Message: Failed to marshall domain instance [com.x.y.z.B : 675]

What is presumably happening is when marshalling B, the object is searching for a FK in A.id when it should actually be looking at A.specialID. I am able to lookup a specialID 300 in DB while there is no 300 in ID of table A. We want instance of B to lookup A.specialID instead of the defaulted A.id. Is this possible? We are not using hasOne mapping since bidirectional mapping was not appropriate in this situation. Appreciate any help with mapping.

Upvotes: 0

Views: 136

Answers (1)

Emmanuel Rosa
Emmanuel Rosa

Reputation: 9885

I'm assuming that the id of domain class A is specialID. If that's the case then you can change the id property from the default like this:

 Class A {
    ...  

    static mapping = {
        id type:Long, sqlType:'INT', name: 'specialID'
    }    
 }

I'm uncertain what the primary key is for table a, but assuming it's... foo, then you can set the column like this:

 Class A {
    ...  

    static mapping = {
        id type:Long, sqlType:'INT', name: 'specialID', column: 'foo'
    }    
 }

You can read more about id mapping in the Grails documentation.

Upvotes: 1

Related Questions