2017-03-03 74 views
-3

我是grails的新手,並可通過在線獲得其文檔進行學習。如何使用HQL與find函數grails(GORM)一起使用select?

有一件事讓我在閱讀find()功能時感到困惑。

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任何問題請評論.....

或 可先回答如何使用SELECT語句find(String query)

謝謝

+0

你試過'的findAll()'? –

回答

1

對於一個結果:

Category.find("from Category cat where cat.id = ?", [5L])?.description 

或者爲倍數:

Category.find("from Category cat where cat.id = ?", [5L]).collect{ it.description } 
+0

但是......這並不是我所期望的答案..但是這個答案值得一票贊成...... –

相關問題