如何在Grails 1.3.7中使用带有对象的对象的namedQueries?

时间:2011-02-22 07:34:33

标签: grails gorm

我有这些课程:

class Category {
  Set usersLinked
  def hasMany = [usersLinked:CategoryUserLink]  
  static namedQueries = {
    getAuthorizedBusiness { auser ->
        eq "business", true
        usersLinked{
            eq "user", auser
            type{
                eq "name", "Authorized"
            }
        }            
    }
  }
}
class CategoryUserLink {
  User user
  CategoryType type
  Category category
}

当我使用namedQuerycreateCriteria查询时,我收到错误消息:

Category.getAuthorizedBusiness(user).list()

没有这样的属性usersLinked

为什么呢?

我收到此错误消息:

 org.hibernate.QueryException: could not resolve property: userslinked of: lli.faqapp.domain.Category
    at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1223)
    at grails.orm.GormLabsHibernateCriteriaBuilder.super$3$invokeMethod(GormLabsHibernateCriteriaBuilder.groovy)
    at grails.orm.GormLabsHibernateCriteriaBuilder.methodMissing(GormLabsHibernateCriteriaBuilder.groovy:65)
    at lli.faqapp.domain.Category$__clinit__closure3_closure4.doCall(Category.groovy:84)
    at grails.orm.HibernateCriteriaBuilder.invokeClosureNode(HibernateCriteriaBuilder.java:1367)
    at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1117)
    at GormLabsGrailsPlugin$_registerCriteria_closure23_closure38.doCall(GormLabsGrailsPlugin.groovy:255)
    at lli.faqapp.domain.tests.CategoryTests.testSearchOnCategories(CategoryTests.groovy:39)

删除插件GormLabs后,我开始进行集成测试,收到此消息:

 org.hibernate.QueryException: could not resolve property: userslinked of: lli.faqapp.domain.Category
    at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1223)
    at lli.faqapp.domain.Category$__clinit__closure3_closure4.doCall(Category.groovy:84)
    at grails.orm.HibernateCriteriaBuilder.invokeClosureNode(HibernateCriteriaBuilder.java:1367)
    at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1117)
    at lli.faqapp.domain.tests.CategoryTests.testSearchOnCategories(CategoryTests.groovy:39)

1 个答案:

答案 0 :(得分:2)

hasMany必须是静态的 - 它是Class的属性,而不是特定的实例。它生成的Set usersLinked是特定于实例的,但hasMany声明适用于Class。