Eddard Stark
Eddard Stark

Reputation: 3595

Custom Join table in grails

I have a following domains

  1. User (in database called usermanagement) and
  2. Account (in another database xyz)

Its an old system so i cannot really change the architecture of the system. I have been assigned task to implement a system that a certain users can only access certain accounts. This is a classic case of many-to-many relationship but the problem lies in the fact that these two domains are in two different databases. I googled if that was possible but i realized that it was not possible. So I now am thinking of creating a custom join table to store info on which user are allowed access to which accounts. The table can have two columns 'accountId' and 'userId'. So for this, do i have to create a new domain in grails or is there any cleaver way of doing this ? Thanks in advance.

Upvotes: 0

Views: 835

Answers (3)

Eddard Stark
Eddard Stark

Reputation: 3595

As I searched for solution of this, I did not find any sustainable solutions. I eventually narrowed down the probable solutions to two: 1. Create a domain table (only) using sql, some sort of patch and use hard-coded queries in grails to write and access data to and from the table. 2. Create a domain class like AccountUser having properties clientId and userId

I choose the 2nd option, I wrote some additional methods and created a service to return user and client instance and I am done ! Anyways, thanks guys.

Upvotes: 1

Aram Arabyan
Aram Arabyan

Reputation: 2359

If you create joinTable in which DB you are going to create it and how you are going handle updates in main (Account,User) tables and ceep your join table up2date ?

I think (for this case) you don't need join table, you need handle business logic in application level.

You cane have 2 Domain classes each one pointed to different dataSource(DataBase).

http://grails.org/doc/latest/guide/conf.html#multipleDatasources

Upvotes: 1

R. Valbuena
R. Valbuena

Reputation: 474

If the databases are "visible" to each other (on the same server or there is a db link between them), you should be able to map the domain classes using the fully qualified table names ('schema.tablename') in the mapping closure.

psuedocode:

class User {

static mapping = {
    table "usermanagement.user"
}

static hasMany = [Account:accounts]

}

class Account {

static mapping = {
    table "xyz.account"
}

}

http://grails.org/doc/latest/guide/GORM.html#tableAndColumnNames

Upvotes: -1

Related Questions