Ivan Klaric
Ivan Klaric

Reputation: 423

GORM where query on an embedded object

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?

Upvotes: 6

Views: 2872

Answers (2)

Ivan Klaric
Ivan Klaric

Reputation: 423

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()

Upvotes: 4

dmahapatro
dmahapatro

Reputation: 50245

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'))

Upvotes: 0

Related Questions