Grails GORM domain association across two datasources

前端 未结 3 1562
無奈伤痛
無奈伤痛 2020-12-15 11:07

Is it possible to have an association between two domain classes (i.e. belongsTo) if the other domain class uses a different datasource? The two datasources ar

3条回答
  •  北海茫月
    2020-12-15 11:50

    I came up with another solution if anyone wants something a bit more "automated" if that applies, maybe it has repercusions in performance but makes it easier to work with specially if you, like me are using MongoDB for Grails plugin, which sometimes makes it neccessary to use the native java API to get documents.

    What I did is that in DomainA, I use beforeValidate() to assign the value of domainClassBId and onLoad() to assign the value of dcB. This way, the flow will be more natural to what everyone is used to with Hibernate. When saving the DomainA, the will assing a DomainB to the DomainA and the onvalidate code will persist only the id to the corresponding datasource. When you load objects, for example with the DomainA.find() method, the onLoad code will make sure that the returned object has a property of type DomainB instead of an Long which you then use for quering the databse. It is basically the same approach denoted before by Burt Beckwith but in a more simple way in terms of saving and loading the DomainA. Again I'm not sure if it has performance issues, that's something I would like someone with more experience could help us with.

提交回复
热议问题