grails - 无法将对象放入我的BootStrap.config中的数据库

标签 grails

我是grails noob,正在关注本教程:http://www.bobbywarner.com/2012/01/05/jump-into-grails-2-0/

我做:
类BootStrap {

def init = { servletContext ->
    def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
    def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)

    def user1 = SecUser.findByUsername('bobby') ?: new SecUser(username: 'bobby', enabled: true,  password: 'password').save(failOnError: true)
    if (user1.authorities.contains(userRole)) {
        SecUserRole.create(user1, userRole, true)
    }

    def user2 = SecUser.findByUsername('tony') ?: new SecUser(username: 'tony', enabled: true,  password: 'password').save(failOnError: true)
    if (user2.authorities.contains(userRole)) {
        SecUserRole.create(user1, userRole, true)
    }

    if (user2.authorities.contains(adminRole)) {
        SecUserRole.create(user1, adminRole, true)
    }

    assert SecUser.count() == 2;
...

但是当我启动时,我得到:
| Error 2013-08-31 22:29:14,027 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Assertion failed: 
assert SecUser.count() == 2
               |       |
               0       false

我检查数据库,并且SecUser表中没有SecUsers。

关于我在做什么错的任何想法吗?

谢谢

最佳答案

正如@grantmc指出的那样,添加flush:true将有所帮助。但是代码还有其他问题。

一种是您应检查!contains而不是contains-如果“用户/角色”授予不存在,则对其进行授予。但是使用的方法效率低下-它返回所有现有记录并检查是否匹配。使用代理时,这也可能会失败,除非您创建良好的equalshashCode方法(尽管此处不是这种情况)。检查该行是否存在要好得多,并且使用GORM很容易:

class BootStrap {

   def init = { servletContext ->
      def adminRole = Role.findByAuthority('ROLE_ADMIN') ?:
                      new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
      def userRole = Role.findByAuthority('ROLE_USER') ?:
                     new Role(authority: 'ROLE_USER').save(failOnError: true)

      def user1 = SecUser.findByUsername('bobby') ?:
                  new SecUser(username: 'bobby', enabled: true,  password: 'password').save(failOnError: true, flush: true)
      if (!SecUserRole.findBySecUserAndRole(user1, userRole)) {
         SecUserRole.create(user1, userRole, true)
      }

      def user2 = SecUser.findByUsername('tony') ?:
                  new SecUser(username: 'tony', enabled: true,  password: 'password').save(failOnError: true, flush: true)
      if (!SecUserRole.findBySecUserAndRole(user2, userRole)) {
         SecUserRole.create(user2, userRole, true)
      }

      if (!SecUserRole.findBySecUserAndRole(user2, adminRole)) {
         SecUserRole.create(user1, adminRole, true)
      }

      assert SecUser.count() == 2
      assert SecUserRole.count() == 3
      assert Role.count() == 2
   }
}

关于grails - 无法将对象放入我的BootStrap.config中的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18553036/

相关文章:

grails - Spring Security编码密码从旧版本到新版本的问题

grails - Grails 1.0.3升级问题

grails - Intellij w/Grails : Cannot resolve symbol for inherited params and methods

grails - Grails <g:消息代码=“”/>和$ {message(code)}之间的区别

grails - 如何在gsp中显示域值

web-services - Grails wslite SOAP参数导致解码错误

tomcat - Grails 错误仅在 Tomcat 服务器上;本地不可复制 : Error executing tag <g:link>: object is not an instance of declaring class

grails - Grails countrySelect标签-默认值

Grails 2.2.3 依赖问题未解决

grails - 无法初始化代理 - 无 session ,正在访问过滤器类中的 session 对象