Let's get in touch

GORM querying with embedded classes

Some guys on the project went for a vacation and there were some urgent fixes to be done on a Grails project. As usual, those fixes couldn’t wait for the guys to return from the vacation and I didn’t want to disturb their beer drinking at the beautiful Croatian coast. And the change request was simple enough. How hard can it be? The task at hand was the following:

  • given a list of 10+ parameters, return the filtered list according to the sent param
  • you must only filter by the params sent

Meh, that means the usual grails Domain.findAllByThisAndThat() magic will be of no use. My first try was using the where queries mechanism. That worked fine until I got bitten in the behind by an embedded property in my domain model. You can find an explanation of my problem on stackoverflow and as you can see, the feedback I got didn’t help me much. So, what did I do? I scrolled down on the GORM manual page 🙂 Criterias were of not much help since they were also compile-time, but DetachedCriteria allows for chaining of criterias in this manner:

and what’s more important, it supports embedded objects in domain classes:

This worked flawlessly. So, I hope Google caches this conclusion out of this post: Don’t use where queries in GORM when you have embedded objects in your domain classes! Use DetachedCriteria instead! Did anyone else stumble upon this issue? If so, how did you fix it?

Leave your comment

We use cookies to help us optimize the website experience. Okay

Learn more about our privacy policy.