grails - org.hibernate.QueryException(无法解析 属性)

grails - org.hibernate.QueryException (could not resolve property)

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

控制器中的代码:

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

我做错了什么?

findAll 不限于调用 class 的实例,所以我改用 executeQuery。

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

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

def list = Rental.findAllByAccount accountInstance

甚至

def list = Rental.findAllByAccount getPrincipal()

如果 getPrincipal() 的 return 类型具有 id 字段。