I'm having an issue where calling .contains()
on one of my domain classes' hasMany
relationships is not doing the same when running normally, or when debugging. The situation is as follows:
I have 2 domain objects, A
and B
. A
has a hasMany
relationship with B
.
class A {
...
static hasMany = [bees: B]
...
}
Now, during the execution of one of my filters, I grab my current user from the spring security service. This user also contains a single instance of B
. What my filter should do is to check if the instance of B
in the user is contained in some instance of A
.
Assume that the instances of B
are actually referring to the same object (since they are).
Now, the issue arises. Calling:
if (instanceOfA.bees.contains(user.instanceOfB)) {
println 'success'
} else {
println 'failure'
}
prints failure
during normal (or debugging without stepping through the code) execution. However, if I put a break-point there, and step through the code, it correctly executes the contains()
and prints success
.
I have also implemented equals
, hashCode
and compareTo
in an attempt to resolve this, but with the same behaviour.
So it seems that using one of the Groovy transform annotations seems to do the trick. Simply adding:
// uid is a uniqe UUID we use to identify with other systems.
@EqualsAndHashCode(includes = ["id", "uid"])
does the trick. Seems a bit strange that the IDE generated methods (using the same fields) did not...