I have a situation where I need to connect multiple DBs to get all the attributes for a single object (poor legacy design). I followed the instructions at http://grails.org/doc/2.0.0.RC1/guide/conf.html#multipleDatasources to hook up the 2nd database, but now I’m a bit stumped on how to map it to my class — telling it which columns come from which tables in which DB.
My primary source is called dataSource, and my secondary is called dataSource_cvs as the instructions in the documentation recommended. (Upon inspection, I see that this section has since been removed from the latest grails documentation at http://grails.org/doc/latest/guide/3.%20Configuration.html)
I’m now wondering what the next step is. I want to know how I can specify using annotations or GORM that the different attributes are being mapped from different DBs, and bring them together. Also, do I have to do anything funny in my hibernate.cfg.xml, such as a separate session-factory for the 2nd database? If so, do I just need to add the mapping class to both session-factories?
Thanks!
ETA: If it helps, the 2nd database (hooked to cvs) only has to be read-only for this particular situation.
As far as I understand it, you can’t map individual domain class properties to multiple data sources. You would have to create two domain classes: one that maps to the read-write database and another than maps to the read-only one. Then set up a relationship between the two.
For convenience in your coding, you could add transient read-only properties to your main domain class:
Note that I’ve made the
readOnlyproperty eager so that you don’t have a second query every time you access thegetName()method for the first time in a session.Whether this is an efficient approach is another question, but I’ll leave that to others.
BTW the section on multiple data sources hasn’t been removed from the user guide. ‘latest’ is currently pointing at the 1.3.7 user guide whereas multiple data source support in core is specific to Grails 2.0. Very soon, ‘latest’ will point to the 2.0 user guide…