【问题标题】:Grails 2.4.4, Multiple DataSources, Using GORM finder on Domain class results in: Method on class [User] was used outside of a Grails applicationGrails 2.4.4,多个数据源,在域类上使用 GORM 查找器导致:类 [User] 上的方法在 Grails 应用程序之外使用
【发布时间】:2015-03-21 09:11:00
【问题描述】:

我正在尝试在 BootStrap.groovy 初始化闭包中添加一个默认用户。这导致了这个错误: [com.exmaple.AdminUser] 类的方法在 Grails 应用程序之外使用。

我的 BootStrap.groovy 文件:

class BootStrap {
   def init = { servletContext ->
      if (!AdminUser.findByEmail("eric@example.com")) {
         AdminUser eric = new AdminUser(
               email: "eric@exmaple.com",
               firstname: "Eric",
               lastname: "Berry",
               password: "password"
         ).save()
         if (eric.hasErrors()) {
            log.error("Error creating admin user: ${eric.errors}")
         }
      }
   }
   def destroy = {
   }
}

我的 DataSource.groovy 文件(相关位):

dataSources {
   dataSource {
      ...
   }
   adminDataSource {
      pooled = true
      jmxExport = true
      driverClassName = "org.h2.Driver"
      username = "sa"
      password = ""
   }
}
hibernate {
   cache.use_second_level_cache = false
   cache.use_query_cache = false
   cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
   singleSession = true // configure OSIV singleSession mode
}

// environment specific settings
environments {
   development {
      dataSources {
         dataSource {
            ...
         }
         adminDataSource {
            dbCreate = "update"
            url = "jdbc:h2:admin_user_db;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
         }
      }
   }
}

还有我的 AdminUser 对象:

class AdminUser {
   String id
   Date dateCreated
   Date lastUpdated

   String email
   String firstname
   String lastname
   String password

   static constraints = {
      id(maxSize: 36)
      email(nullable: false, blank: false, email: true, unique: true)
      firstname(nullable: false, blank: false)
      lastname(nullable: false, blank: false)
      password(nullable: false, blank: false)
   }

   static mapping = {
      datasource('adminDataSource')
      id(generator: 'uuid2')
   }

   def beforeInsert() {
      encodePassword()
   }

   def beforeUpdate() {
      if (isDirty('password')) {
         encodePassword()
      }
   }

   private void encodePassword() {
      password = BCrypt.hashpw(password, BCrypt.gensalt(12))
   }
}

最后,我的休眠版本是(来自 BuildConfig.groovy):

runtime ":hibernate4:4.3.6.1"

我正在另一个 Grails 应用程序中做类似的事情,使用相同版本的 Grails 和 Hibernate,并且那个工作正常。我可以看到的两个区别是:

  1. 我没有使用多个数据源
  2. 正在运行的应用程序使用服务来创建默认用户,该用户被注入到 BootStrap.groovy 文件中。

我不确定我在这里做错了什么,或者如何解决它。

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: grails


    【解决方案1】:

    这是因为您在 DataSource.groovy 文件中使用了错误的数据源名称 (adminDataSource)。

    在多数据源中,所有数据源名称必须有前缀dataSource_,默认数据源除外。

    因此,只需将您的数据源名称从 adminDataSource 更改为 dataSource_adminDataSource

    【讨论】:

    • 错误仍在发生。文档没有提到这样的事情。 grails.github.io/grails-doc/latest/guide/… 在示例中,第二个数据源名为“lookup”,而不是“dataSource_lookup”。
    • 更正!这确实解决了它,非常感谢!我正在查看最新版本的 Grails 的文档,结果是 3.0.0RC2。看起来他们正在改变多个数据源的完成方式。 Grails 2.4.4 的文档显示了您所描述的示例。 grails.github.io/grails-doc/2.4.4/guide/…
    猜你喜欢
    • 2014-02-21
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多