I’m using the datasources pluging for Grails described here: http://burtbeckwith.com/blog/?p=70
I’m connecting to 2 MySQL database schemas on the same server: my_schema_1 and my_schema_2. Most of the data I need comes from my_schema_1, but one of its tables contains a column that references one of the tables in my_schema_2.
Here are my datasource definitions in my Datasources.groovy file (simplified):
datasources = {
datasource(name: 'my_schema_1') {
domainClasses([Question, Answer])
driverClassName('com.mysql.jdbc.Driver')
url('jdbc:mysql://test.myserver.com/my_schema_1')
username('***')
password('***')
}
datasource(name: 'my_schema_2') {
domainClasses([Genre])
driverClassName('com.mysql.jdbc.Driver')
url('jdbc:mysql://test.myserver.com/my_schema_2')
username('***')
password('***')
}
}
Here are my 3 class definitions:
class Question {
String text
Answer answer
Genre genre
}
class Answer {
String text
}
class Genre {
String name
}
Whenever I try to perform a criteria query on the Question class, I get the following mapping exception:
An association from the table question refers to an unmapped class: Genre
If I comment out the genre property in the Question class, everything works fine. If I perform a criteria query on the Genre class itself,
everything works fine. There just seems to be a problem joining the 2 classes across schemas. (Of course, it’s also very possible I missed
something or did something incorrectly.)
Am I doing something wrong or is this a limitation of the datasources plugin? And, if this is a limitation of the plugin, what alternatives could I use to achieve what I need?
Any help/suggestions are much appreciated.
Thanks,
B.J.
I found a simpler solution since the databases are on the same server.
I simply define one datasource as follows (without specifying a database):
Then I specify the database in my domain classes’ mapping sections:
Again, this only works because the 2 databases are on the same server, and they use the same username/password.