I am building a grails application with domain classes whose id generator is assigned (that is, they do not auto-generate). Assume I have a domain class Parent with static hasMany = [children: Child] and domain class Child.
I am trying to locate an instance of Parent using Parent.findWhere(propertyMap), but am getting a NonUniqueObjectException indicating that there is already a Child instance associated with this session having id 0. My propertyMap does not contain an entry for id.
Now, I am used to getting this error any time I try to save two instances of one class in a session without setting their ids. Because ids are not auto-generated, they default to zero, which allows the first to be saved, and the second throws an error, as the id is a duplicate of the first. What is surprising me here is that I am trying to find an instance, not save one… Does anyone know what might be causing this behavior and how I can get around it while still searching using my property map?
It turns out this was a groovy syntactical sugar problem. I was calling findWhere like
but apparently I should be using
(note the lack of square brackets). I don’t know why this made a difference, but findWhere works as expected now. Extra points to anyone who might know why it behaves like this.