【问题标题】:grails - org.hibernate.QueryException (could not resolve property)grails - org.hibernate.QueryException(无法解析属性)
【发布时间】:2017-09-30 21:57:45
【问题描述】:

我正在尝试从当前用户那里获取特定租赁的列表。

控制器中的代码:

def index() { 
if (isLoggedIn()) {
    String username = getPrincipal().username

    def accountInstance = Account.findByUsername(username)
    def rentalInstanceList = Rental.findAll("from Rental as r where r.account_id=:accountid", [accountid: accountInstance.id])
    }
}

account_id 是外键。

运行后报错:

could not resolve property: account_id of: ers.Rental

我做错了什么?

【问题讨论】:

  • 我们可以查看您的租赁域类吗?

标签: grails hql


【解决方案1】:

通常,在 HQL 中,您必须使用域类中定义的字段名称。因此,您的查询应如下所示:

def list = Rental.findAll("from Rental where accountId=:accountid", [accountid: accountInstance.id])

def list = Rental.findAllByAccount accountInstance

甚至

def list = Rental.findAllByAccount getPrincipal()

如果getPrincipal() 的返回类型有id 字段。

【讨论】:

    【解决方案2】:

    findAll 不限于调用类的实例,所以我改用 executeQuery。 https://stackoverflow.com/a/8916483/5011228

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      相关资源
      最近更新 更多