I am working with Grails and MongoDB. I have two domain classes User and AddWebsite. A User hasMany websites and each website belongs to a user. The domain classes are as follows:
class AddWebsite{
String website
User user
static belongsTo = [user: User]
static constraints = {
website url:true
user nullable:true
}
}
The Other domain class is as follows:
class User {
String login
String password
static hasMany = [
addWebsites: AddWebsite
]
static mapping = {
addWebsites cascade:"all-delete-orphan"
}
static constraints = {
}
}
I need to query the AddWebsite table based on the current logged in user and get the websites of that particular user. Can anyone suggest any approach?
You need to create a createCriteria List as follow
def c = AddWebsite.createCriteria()
def results = c.list {
//find the user based on the relationship
user {
ideq(userobj?.id.toLong())
}
//you can user projection here if u need a single value
}