Multilple datasources with association in grails

55 views Asked by At

In my present project I need to use 2 databases, one for master data and the other for client data.As of now we are using single db but we planned to split it into 2 databases (master db & client db).

We have association between client and master db on domain classes level. I tried grails 2.X multiple data-source option, but association is not possible. If any one worked on this scenario suggest me the best approach.

We tend to use single master db for all client instances.

1

There are 1 answers

0
Michal_Szulc On

I based my solution on this answer:

class ClassB {
    Long classAId

    static constraints = {
        classAId nullable: true
    }

    static mapping = {
        datasource 'other'
    }

    static transients = ['classA']

    ClassA getClassA(){
        classAId ? ClassA.get(classAId) : null
    }

    ClassB(classAId){
        def test = ClassA.get(classAId)
        classAId = test ? test.id : null
    }
}

maybe it's not pretty but it's working.