【发布时间】:2012-01-30 23:49:39
【问题描述】:
我有这样的课:
class User {
String name;
String password;
static hasMany = [ authorities : testing.Role ]
static belongsTo = Role
static constraints = {
name(unique:true,size:4..10)
}
}
我已经配置了 Spring Security Plugin(acegi) 并且它按预期工作。但问题是,当我将User 类中的belongsTo 更改为类似这样的内容时:
static belongsTo = [ Role , company : Company]
我尝试运行 grails run-app 我得到这样的错误:
/home/anto/anto/test/testing/grails-app/domain/testing/User.groovy: 7: Unexpected node type: EXPR found when expecting type: LABELED_ARG at line: 7 column: 25. File: /home/anto/anto/test/testing/grails-app/domain/testing/User.groovy @ line 7, column 25.
static belongsTo = [Role , company : Company ]
^
1 error
为什么这个语法会抛出错误?我哪里做错了?
编辑:
现在在得到少数人的回答后,我将代码更改为:
static belongsTo = [ role: Role , company : Company]
但这次我遇到了更大的错误!
2011-12-31 14:23:57,304 [Thread-9] ERROR context.ContextLoader - Context initialization failed
Message: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User
Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 138 | run in java.util.concurrent.FutureTask
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 662 | run . . in java.lang.Thread
Caused by GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 138 | run in java.util.concurrent.FutureTask
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 662 | run . . in java.lang.Thread
| Error 2011-12-31 14:23:57,401 [Thread-9] ERROR context.GrailsContextLoader - Error executing bootstraps: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User
Message: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.codehaus.groovy.grails.exceptions.GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User
Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 138 | run in java.util.concurrent.FutureTask
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 662 | run . . in java.lang.Thread
Caused by GrailsDomainException: No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 138 | run in java.util.concurrent.FutureTask
| 886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 662 | run . . in java.lang.Thread
现在,发生了什么事?
但如果我这样做:
static belongsTo = Role
一切正常。而且不知道为什么之前的语法会抛出这样的错误!
我是 Grails 的初学者,我需要帮助。
注意: 我使用的是 Grails 2.0
提前致谢。
【问题讨论】:
-
Ant 你应该阅读你的堆栈跟踪并在文档中搜索解决方案。您是初学者,但首先要自己搜索,然后在 StackOverflow 上寻求帮助。这就是原因:
No owner defined between domain classes [class testing.Role] and [class testing.User] in a many-to-many relationship. Example: static belongsTo = testing.User,您应该在这里寻找解决方案:grails.org/doc/latest/guide/GORM.html#manyToMany -
@TomaszKalkosiński:我知道这是问题所在。但我想知道,如果这行得通
static belongsTo = Role但为什么static belongsTo = [ role : Role, company : Company]这行不通并抛出错误!!。那是我的疑问。
标签: grails groovy spring-security grails-plugin