Parent-child query in 2.x

45 views
Skip to first unread message

Richard Rodseth

unread,
Mar 2, 2015, 8:41:59 PM3/2/15
to scala...@googlegroups.com
I have a domain object represented by two tables with a 1 to many parent child relationship,
and wish to return a complete object containing the parent attributes and a list of the children. 

I found this question and Christopher's answer using groupBy:


Are there any samples showing how to address this very common use case?

Thanks.


Richard Rodseth

unread,
Mar 3, 2015, 11:01:35 AM3/3/15
to scala...@googlegroups.com
Here's the example from that link:

def findParentAndNumberOfChildren(parentField: String)
   = parents.filter(_.parentField === parentField)
        .leftJoin(children).on(_.id === _.parentId)
        .filter{ case(parent,child) => /* your filters */ }
        .groupBy{ case(parent,_) => parent }
        .map{ case (parent,group) => (parent,group.size) }
        .firstOption

In my case I don't want the group list contents not its size, and would like to map the whole thing to a case class.
In a single table, I have the projection operator to do the case class mapping, but I'm unclear how to do it here.
The types are very confusing.

Any help appreciated.

Richard Rodseth

unread,
Mar 5, 2015, 10:35:28 AM3/5/15
to scala...@googlegroups.com
Any takers?
Reply all
Reply to author
Forward
0 new messages