Can anyone see a problem with QBE here?

dave

Administrator
Staff member
Having an issue using Query By Example to retrieve a particular record and not sure why it's behaving as it is. After looking at the generated SQL, it appears it's not actually querying on the fields I'm populating on my example object.

E.g.

def exampleObject = new User(account:account,product:product)
def foundObject = User.find(exampleObject)

// this returns null and the SQL doesn't even look at the fields I've passed in..

However the criteria search below behaves correctly and returns the expected record(instance)

def foundObject = User.withCriteria {
eq('account',account)
eq('product',product)
}?.get(0)

A little puzzling, but i'm wondering whether QBE only works with basic field types and not associations.
 

WillK

New member
It seems like a subtle difference but this syntax should work:
def foundObject = User.find(new User(account:account,product:product))
 

dave

Administrator
Staff member
That's what I was originally doing, but then split it over different lines just to make things clearer. It's a strange one alright.
 

WillK

New member
what about using the User.findByAccountAndProduct querying, if that works it's still a little less code than criteria
 

dave

Administrator
Staff member
Ahh well in my application I'm actually having to query on three elements, So I couldn't do that either pah.

The actual code I'm using now is:

def foundProducts = AgreementProduct.withCriteria {
eq('product',product)
eq('agreement',agreement)
if (recipient)
eq('recipient',recipient)
else
isNull('recipient')
}

Totally different object names than in my question above, but the principle I would have thought should be the same.

It's a few extra lines than I planned on, but I was just niggled that I couldn't figure out with the QBE wasn't working as expected.

what about using the User.findByAccountAndProduct querying, if that works it's still a little less code than criteria
 
Top