I have a 'User' and a 'UserOrder' class in grails. There is a 'belongsTo' relationship defined on the userOrder class and a hasMany relationship on the 'User' class, like so:
class User {
String username
String password
static hasMany = [orders:UserOrder]
Set<UserOrder> getUserOrder() {
return orders.findWhere(status: 0)
}
}
class UserOrder {
User user
Integer status
static belongsTo = [User]
}
My issue is that the 'getUserOrder' method doesn't work. I just want it to return the first (any) UserOrder with the status set to '0', but when I run the above code, I get the following error:
No signature of method: org.hibernate.collection.PersistentSet.findWhere() is applicable for argument types: (开发者_如何学Cjava.util.LinkedHashMap) values: [[status:0]]
How do I get the method to just return a UserOrder, belonging to this User with a status of '0'?
You can't do effective filtering on collection property - all the collection will be fetched first. Plus, findWhere()
is a static method. For effective DB querying, just use
UserOrder getUserOrder() {
return UserOrder.findWhere(user: this, status: 0)
}
Note the return type change. To get a Set
of matching orders, use
UserOrder.findAllWhere(user: this, status: 0)
精彩评论