使用SELECT语句
我是Grails的新手,并通过其文档在线学习。
一件事使我在阅读有关()函数时都感到困惑。
Book.find(String query) // pass the HQL, However there are various overloaded variants.
现在,如果我使用此find()
函数。
Category.find("from Category as cat where cat.id = ? ", [5L]) //result is fine.
我的类别类
class Category {
String title;
String description
String image
static constraints = {
title blank: false, nullable: false
description blank: true, nullable: true
}
}
,但是如果我尝试了此HQL查询
Category.find("select cat.description from Category as cat where cat.id = ? ", [5L])
//or this query.
Category.find("select description from Category as cat where cat.id = ? ", [5L])
它引起了例外: -
Invalid query [select cat.description from Category as cat where cat.id = ? ] for domain class [class com.ecommerce.domain.Category]. Stacktrace follows:
Message: Invalid query [select cat.description from Category as cat where cat.id = ? ] for domain class [class com.ecommerce.domain.Category]
所以,如果我的HQL有任何问题,请评论.....
或您可以回答如何使用find(String query)
??
对于单个结果:
Category.find("from Category cat where cat.id = ?", [5L])?.description
或倍数:
Category.find("from Category cat where cat.id = ?", [5L]).collect{ it.description }