无法在BootStrap.config中将对象导入数据库

时间:2013-08-31 21:34:21

标签: grails

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

我这样做: 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)
    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表中没有SecUser。

关于我做错的任何想法?

由于

2 个答案:

答案 0 :(得分:3)

正如@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
   }
}

答案 1 :(得分:1)

这不是一个答案,而是一个关于如何使用findOrSaveWherefindOrSaveBy*在很大程度上减少代码的意见。

class BootStrap {
   def init = { servletContext ->
      def adminRole = Role.findOrSaveByAuthority('ROLE_ADMIN')
      def userRole = Role.findOrSaveByAuthority('ROLE_USER')

      //findOrCreateBy would be lengthy here as well, so back to original code
      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)
      }

      //findOrCreateBy would be lengthy here as well, so back to original code
      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
   }
}