【问题标题】:Spring Security Plug-in Doesn't Work With More Than One BelongsTo. Why?Spring Security 插件不能与多个 BelongsTo 一起使用。为什么?
【发布时间】: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


【解决方案1】:

应该是static belongsTo = [role: Role , company : Company ]

【讨论】:

    【解决方案2】:

    这根本不是安全问题,而是 GORM 问题。 [ Role , company : Company] 没有意义,因为您将 Groovy 的 List 语法 ([Foo, Bar, Baz]) 与其 Map 语法 ([foo: Foo, bar: Bar, baz: Baz]) 混合在一起。你必须选择一个。

    【讨论】:

      【解决方案3】:

      堆栈跟踪似乎表明问题出在关系的另一端。 Role 和 Company 类的外观如何?他们都应该有一个像“用户用户”这样的属性

      【讨论】:

        猜你喜欢
        • 2023-01-30
        • 2021-12-09
        • 1970-01-01
        • 2014-01-18
        • 2015-07-16
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        • 2016-04-29
        相关资源
        最近更新 更多