As @dmahapatro points out in his comment, this is like a 1-element case, and creating your own relationship management methods is the way to go. This is also related to a conversation I made some time ago about performance issues with matching collections, so you can kill two birds with one stone: http://www.infoq.com/presentations/GORM-Performance
If you do not need a collection, i.e. if you use it only to add new instances of the child, this will work because the get call on the DomainClassB instance will use its data source:
class DomainClassA { Long domainClassBId private DomainClassB dcB DomainClassB getDomainClassB() { if (!dcB && domainClassBId) { dcB = DomainClassB.get(domainClassBId) } dcB } void setDomainClassB(DomainClassB dc) { domainClassBId = dc.id } static transients = ['domainClassB'] static mapping = { datasource "ds1" table name: "domain_class_A", schema: "schema_A" } } class DomainClassB { static mapping = { datasource "ds2" table name: "domain_class_B", schema: "schema_B" } }
Creating a new instance of DomainClassA is slightly different from the traditional addTo... approach, but it's not so bad:
DomainClassB dcb = ... def dca = new DomainClassA(domainClassBId: dcb.id) dca.save()
If you want to access all instances of DomainClassA for DomainClassB , you can add a method for this:
Set getDomainClassAs() { DomainClassA.findAllByDomainClassBId(id) }
But since you make the request yourself, you do not need to download all instances if you only need some, so you can make any requests that you want.
Burt beckwith
source share