Tuesday, 10 September 2013

Quering two tables in grails

Quering two tables in grails

pI have two domain classes in my grails project. the first one is user and
second one is contact. the user has one to many relationship with the
contact class, i.e one user has many contacts. the user class is like
this/p precodepackage contacts class User { String name String email
String password static constraints = { name(nullable: false)
email(nullable: false,email: true,blank: false ) password(nullable:
false,size: 6..8,blank: false,password:true) } static hasMany = [contacts:
Contact] String toString(){ return name } } /code/pre pand the contact
class is like this/p precodepackage contacts class Contact { String
firstName String lastName String email String phone String address Date
dateCreated static constraints = { firstName(nullable: false)
lastName(nullable: true) email(nullable: false) phone(nullable: true)
address(nullable: true) dateCreated() } static belongsTo = [user: User] }
/code/pre pwhen I compile this, it creates two tables named user and
contact, the contact table has user_id as foriegn key from user table
which is called id in user table. Now I want retrieve all the contacts of
some specific user. I am wondering how to do this. I have tried different
approuches of dynamic quering but failed. can anybody help me to solve
this.. Thanks in advance/p

No comments:

Post a Comment