Reputation: 37
I'm creating an application with Grails.
I have two domain classes:
parent and child:
class Parent {
string name
hasMany[children: child]
}
class Child {
string name
belongsTo[parent: parent]
}
I search for all the parents with a name similar to a keyword:
def parents = Parent.findAll("From Parent as parent where parent.name like '%fra%'")
I'd like to query the database searching for all children that have a parent in the parents list.
How I could I accomplish that ?
Thanks
Upvotes: 1
Views: 257
Reputation: 1733
There are a lot of possibilities, see http://grails.org/doc/latest/guide/GORM.html#querying
You could also query the children via:
def children = Child.where {
parent.name ==~ "%fra%"
}.findAll()
Upvotes: 0
Reputation: 50245
Using HQL you can achieve the same in a single query:
def children = Child.executeQuery("select c from Child as c \
where c.parent.name like '%fra%'")
Upvotes: 1