2016-07-23 60 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' 
}