6

I have domain classes A and B as follows:

class A {
    String prop1
    String prop2
    B prop3

    static embedded = ['prop3']
}

class B {
    String prop4
    String prop5
}

When I want to query like this:

def q = A.where { prop3.prop4 == 'bla' }
def list = q.list()

I get the following exception:

Cannot get property 'javaClass' on null object. Stacktrace follows:

on the "def q = A.where ..." line.

Any clue what's the problem? I've checked this:

http://grails.1312388.n4.nabble.com/GORM-embedded-object-issue-td1379137.html

but how to "just call them directly" is not quite clear to me. Any other way of querying the embedded objects in GORM?

Ivan Klaric
  • 413
  • 1
  • 4
  • 12

2 Answers2

4

I finally gave up on the where query and went with the DetachedCriteria approach. Gives me the same flexibility as the where queries, but works with embedded domain objects:

def criteria = new DetachedCriteria(A).build {
    eq 'prop1', 'bla2'
}
criteria = criteria.build {
   eq 'prop3.prop4', 'bla'
}
def list = criteria.list()
Ivan Klaric
  • 413
  • 1
  • 4
  • 12
0

What do you get if you do (assuming B is in src/groovy)

def q = A.where { prop3 == new B(prop4: 'bla') }
def list = q.list()

Embedded components are persisted inside the main domain class (owner) itself. It can be accessed directly using any dynamic finder as you do directly on a domain object.

The above can also be represented in dynamic finders as:

A.findAllByProp3(new B(prop4: 'bla'))
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • It doesn't work - it parses it but I get zero results. Most probably because it's missing prop5 (which I don't know and can't filter on). Any other suggestions? Also, how would I do an "IN" query on prop3? – Ivan Klaric Aug 14 '13 at 15:41