0

I am new to grails and I am currently using grails 3.0. I was trying to have the one-to-many relation between two tables across different databases. I have referred almost all the links and grails documentation, but couldn't figure out if this scenario is at all possible. I have sample code written, which works perfectly if the two tables from two different databases are independent. i.e. there is no relation/mapping or references. I see the data has been saved properly in each table through the sample. If I try to map the tables with say one-to-many relation (typical Book-Author domain class example), it fails. I get the mapping exceptions. I also tried the suggested workaround of overriding the get and set methods. Are there any links or sample code which will guide me? Here is my code without getter and setter:

class Book {
    Long   id
    String title
    String author
    Users user
    static constraints = {
        title(blank: false)
        author(blank: false)
    }
}
class Users {
    String username
    String address
    static hasMany = [books: Book]
    static constraints = {
        username(blank: false)
        address(blank: false)
    }
    static mapping = {
        datasource 'lookupusers'
    }
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • How does it fail? How have you configured your Datasoruces? You need to provide more information. – Joshua Moore Feb 09 '16 at 15:47
  • I am using MySQL and created the two databases on the same servers. I see no issues with the datasources as the above code works perfectly if I remove Users object reference from Book class or use default database. I have read in few forums that relation between tables across different databases is possible only through the get and set method in domain class. Here are few links I referred: http://stackoverflow.com/questions/15282879/grails-multi-datasource-domain-issue or http://stackoverflow.com/questions/18768667/grails-gorm-domain-association-across-two-datasources – Sujata Agashe Feb 09 '16 at 17:19
  • Error I get is this -> Caused by: org.hibernate.MappingException: An association from the table book refers to an unmapped class: org.example.Users – Sujata Agashe Feb 10 '16 at 04:01

0 Answers0