2016-07-23 57 views
1

我剛剛安裝了Grails 3.2.0.M2並創建了一個名爲Group的新域名類。然後我跑了generate-all *命令,並試圖瀏覽到GroupController,但這個錯誤迎了上去:什麼詞是無效的域名類

URI:  /group/index 
Class:  org.h2.jdbc.JdbcSQLException 
Message: null 
Caused by: Syntax error in SQL statement "SELECT COUNT(*) AS Y0_ FROM GROUP[*] THIS_ "; expected "identifier"; SQL statement: select count(*) as y0_ from group this_ [42001-192] 

這是發生在這裏:

def index(Integer max) { 
    params.max = Math.min(max ?: 10, 100) 
    respond Group.list(params), model:[groupCount: Group.count()] // Error occurs here 
} 

奇怪的是,這個問題就會消失,如果我將domain class和controller分別重命名爲GroupzGroupzController。爲什麼我不能命名我的域類組?還有哪些其他域名對於域名是非法的?

回答

2

您可以在Grails wiki上找到保留字列表。

或者,您也可以通過使用該static mapping property重命名你的類解決這個問題:

class Group { 
    ... 
    static mapping = { table 'my_group' } 
    ... 
} 
+1

您也可以繼續使用保留字,但它括​​在反引號字符;這將使Hibernate使用正確的引用/轉義數據庫,如[這個較舊的答案](http://stackoverflow.com/a/9468002/160313)所示。 –

0

那麼所有的域類進行掃描和以下方式

用戶轉換成的名字 - >用戶 UserMapping - > user_mapping 等等。

Basicall the camel case is String被大寫字母分割,然後被_分開。

You cannot use any Domain class name that results in the >restricted/reserved words in sql.

e.q group, select, where, from, join, etc

,如果你仍然想使用Grails的這些名字,那麼你可以更改使用mapping塊表名。 Grails將參考您的域/實體類的名字,但在內部將其綁定到映射中指定

e.g

static mapping { 
    table : 'my_table_name' 
}