Jacopo
Jacopo

Reputation: 37

Grails querying the database against a list of objects

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

Answers (3)

Martin Hauner
Martin Hauner

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

dmahapatro
dmahapatro

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

tim_yates
tim_yates

Reputation: 171074

Does:

Child.findAllByParentInList( parents )

do it?

Upvotes: 2

Related Questions