tags:

views:

33

answers:

1

I have a many-to-many relationship between two objects with a join table. I need to be able to select 5 random children based on Parent and date, excluding some children records. I am stuck. Any ideas?

   Parent {
      static hasMany = [children: Child]
    }

    Child {
      Date dob
      static belongsTo = [Parent]
      static hasMany = [parents: Parent]

      static namedQueries {

        randomFiveChildrenBornAfter { parentid, dob, excludeChildren->

          qt 'dob', dob            
          parents {
            eq 'id',parentid
          }
          // not in(excludeChildren) ?? order by rand() ??
        } 
      } 
   }
+1  A: 

Are these literally Parent/Child relationships (as in humans)? If so, the set of children is likely to be very small and I'd probably just do it in memory rather than through a sql query.

parent.children
    .findAll { dob >= fiveYearsAgo }
    .sort { Math.random() }
    .with { it.size() >= 5 ? it.subList(0,5) : it }
Ted Naleid
Thanks Ted for your response. Unfortunately its not literal and a parent object can have thousands of children so it will get rather expensive doing it in memory... Any way to do it with a query using criterias?
Micor
In that case, you'd have to do it in the database. Unfortunately, there isn't a database agnostic way of getting a random row. See this stackoverflow post for details on how to write an HQL criteria that gets random rows with a limit: http://stackoverflow.com/questions/2810693/hibernate-criteria-api-get-n-random-rows
Ted Naleid
Neat trick with sqlRestriction. It will have to do. Thanks.
Micor