hasOne and BelongsTo domain association with multiple datasources in grails 2.x

296 views Asked by At

I am currently working on a scenario where i have one centralized database and 2 extended databases in different regions i.e US and UK. Site and TestSubjectPublic are in centralized database whereas Document is in extended databases. TestSubjectPublic has hasOne and BelongsTo relationship with document. I have overridden the getter and setters but for some reason whenever i fetch the data it sends testSubjectResult as null. Following are the domain classes

class Site  {

    String name
    String notes
    String email
    String phone
    String creditCardNumber
    String chargeForTest


    Boolean isEnabled = true
    Byte deleted = 0
    Byte taxExempt =1

    static hasMany = [testSubjects: TestSubjectPublic]

    Float discountAmount

    SiteType type

    AccountStatus accountStatus=Site.AccountStatus.ACTIVE

    Date accountClosingDate

    Long fedexTrackingNumber

    Date dateFedexNumberAdded

    String signUpToken

    Integer monthOfMoreThanFifteenTests

    Date dateSignUpInviteSent

    String vatNumber
    String website

    static constraints = {
        name(unique: true, nullable: false, blank: false)
        email(email: true, nullable: false, blank: false)
        phone(nullable: true)
        notes(maxSize: 5000, nullable: true)
        creditCardNumber(nullable: true)
        chargeForTest(nullable: true)
        accountStatus(inList: AccountStatus.values().toList())
        accountClosingDate(nullable: true)
        type(inList: SiteType.values().toList())
        fedexTrackingNumber(nullable: true)
        dateFedexNumberAdded(nullable: true)
        signUpToken(nullable: true)
        monthOfMoreThanFifteenTests(nullable: true)
        dateSignUpInviteSent(nullable: true)
        vatNumber(nullable: true)
        taxExempt(nullable: false)
        website(nullable: true)
        discountAmount(nullable: true)

    }

    enum AccountStatus {
        ACTIVE, INACTIVE
    }

    enum SiteType {
        SCHOOL, GENERAL_PRACTITIONER, OTHER
    }
    static mapping = {
    }
}

TestSubjectPublic.groovy

import groovy.transform.ToString

@ToString(includeNames=true)
class TestSubjectPublic {

    String emailId
    String telephoneNumber
    //Specific name/id given in site
    String sitePatientId
    String dialingCode
    //Reference number in QbTest
    String qbTestPatientId

    TestSubjectStatus status
    Byte deleted = 0
    static belongsTo = [site: Site]

    static constraints = {
        site(nullable: true)
        sitePatientId(nullable: true)
        qbTestPatientId(nullable: true)
        dialingCode(nullable: true)
        status(inList: TestSubjectStatus.values().toList())
        emailId(nullable: true)
        telephoneNumber(nullable: true)
    }
    enum TestSubjectStatus {
        NOT_COMPLETED, COMPLETED
    }

    static mapping = {

    }
}

Document.groovy

class Document {
    Long testSubjectPublicId
    private TestSubjectPublic testSubjectPublic
    String filename
    byte[] filedata
    enum DocType {
        RAW,PG,JSON
    }
    DocType type
    Date uploadDate = new Date()

    TestSubjectPublic getTestSubjectPublic() {
        if (!testSubjectPublic && testSubjectPublicId) {
            testSubjectPublic = TestSubjectPublic.get(testSubjectPublicId)
        }
        testSubjectPublic
    }

    void setTestSubjectPublic(TestSubjectPublic tsp) {
        testSubjectPublicId = tsp.id
    }

    static transients = ['testSubjectPublic']

    static constraints = {
        filename(blank:false,nullable:false)
        filedata(blank: true, nullable:true, maxSize:1073741824)
        testSubjectPublic(nullable: true)
    }
    static mapping = {
        datasources(['virginia','ireland'])
        table name: "document"
    }
}

When I try to list the Document

listItems = Document.virginia.list(params)

the testSubjectPublic object is null

Here is my datasource.groovy and currently i am on Dev environemnt

  development {
    dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/abc838?zeroDateTimeBehavior=convertToNull"
        username = "root"
        password = "root"
    }
    dataSource_virginia {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/abc8382?zeroDateTimeBehavior=convertToNull"
        username = "root"
        password = "root"
    }
    dataSource_ireland {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/abc838ireland?zeroDateTimeBehavior=convertToNull"
        username = "root"
        password = "root"
    }
}
0

There are 0 answers