views:

795

answers:

1

Hi,

let's go straight to the problems (with Grails 1.1.1, it should work on previous one)

I have 2 domains ie: User and Detail like this :

Class User {
   String userName ; 
   ..... // another fields 

   static hasMany = [details:Detail];
}

Class Detail{
  String detailName ;
  ... // another fields 

  static belongsTo = [user:User];
}

Now if I did :

def user = User.get(1);
Detail.findAllByUser(user);

why it produce error ?

But if i do modification on Detail

Class Detail{
      String detailName ;
      ... // another fields 

      User user; 
      static belongsTo = [user:User];
    }

(by adding user) it will work like normal ...

is there any effect using belongsTo ? or i did mistakes concept in here?

+1  A: 

Your example is not how you would typically access the Details. The Details would be accessed via the User instance, for example:

def user = User.get(1)
def userDetails = user.details   // not Detail.findAllByUser(user);
John Wagenleitner
ok, it is reasonable for hasMany. how about belongsTo? do i need to state that if i have type hasMany in the User?
nightingale2k1
No, you don't have to have a belongsTo in the Detail class. However, if you omit it then deletes wont be cascaded. For example, if you delete a User you probably want to have GORM automatically delete all Detail records associated with that User. In order to have that managed automatically you need the belongsTo in Details. In your situation I would keep the belongsTo.
John Wagenleitner
okay thanks John. it help a lot
nightingale2k1