Tuesday, March 29, 2011

Multi-Associative GORM Criteria with Projections

I have three domain classes: Lead, Submission and BuyerLog:

I have a need to get the number of duplicate leads which share the same leadBuyer (in the BuyerLog domain class). Here is the SQL:

I want to do this using GORM / Criteria Builder. Here's my final code:

Thanks to schmolly159 on the #grails freenode IRC channel for the examples and continued help.

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Manuel, yes that is correct. I fixed it. Thanks.

    ReplyDelete
  3. Hey Eric,

    Looks very cool, but have you thought about perhaps writing this as a named query inside your domain class? Encapsulating this in your model is a very tidy way of doing this sort of thing.

    Coincidentally I wrote a blog post about this sort of thing too last week! Check out my article here:
    http://mycodesnippets.com/2011/03/20/named-queries-with-projections-in-grails/

    Cheers,
    Marco.

    ReplyDelete
  4. Sorry, I removed the previous comment because I realized it had a typo. :)

    ReplyDelete