I use Grails 2.2.4 (same behaviour in Grails 2.3.11) and have a domain class A that references a domain class B
class A {
static hasOne = [b: B]
static constraints = { b nullable: true }
}
class B {
static belongsTo = [a: A]
}
I try to find all instances of A that have a B.
A.findAllByBIsNotNull()*.b
returns a list of Bs and nulls:
[null, null, b1, b2, null, ...]
How so?
Same happens if I use
A.withCriteria {
isNotNull 'b'
}*.b
What do I do wrong?
UPDATE:
I realized that the problem is because of the hasOne
. If instead of static hasOne = [b: B]
, there is B b
, it works. The former moves the foreign key to table B, the latter creates the foreign key relation in table A.
So why does the querying not work in the former case and how can I query for all A
s, not having a B
when the foreign key is within B?
println A.findAllByBIsNotNull()*.b*.id
? – Frisse