how to define an object / domain that related to another account ?

View: New views
2 Messages — Rating Filter:   Alert me  

how to define an object / domain that related to another account ?

by adwin grails :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have Account domain that may have relationship with another Account.
But the Account may stand alone as well (means has no relationship)

I defined it like this :

class Account{
    String info ;
    .....

    static hasMany = [relatedAccount:Account] ;
}

in some case, I need to create 2 account on the same time :
def firstAccount =  new Account() ;
....

def secondAccount = new Account();
...

firstAccount and secondAccount are related.

my question.
1. is this correct one to save the relationship between 2 new account ?
firstAccount.addToRelatedAccount(secondAccount);
secondAccount.addToRelatedAccount(firstAccount);

firstAccount.save(flush:true);
secondAccount.save(flush:true);

2. how to delete both accounts without creating an exception ?

thank you ...

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: how to define an object / domain that related to another account ?

by DaveKlein :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

A couple questions: Will an Account have more than one related Account?  Will one of the Account instances be the owner (or primary account)? 

If an Account will have zero or one related Account then you could just do something like this:

class Account{
    ...
    Account relatedAccount

    static constraints = {
        ...
        relatedAccount(nullable:true)
    }
}

If that's not the case, then please post more details.

Thanks,
Dave



--
Grails: A Quick-Start Guide
http://pragprog.com/titles/dkgrails
http://gquick.blogspot.com