Grails Gorm 多租户设置无法正常工作

问题描述 投票:0回答:0

我有一个具有多租户设置的 grails 项目,但无法正常工作 项目配置

grailsVersion=5.0.0
grailsGradlePluginVersion=5.0.0
groovyVersion=3.0.7
gormVersion=7.1.0

如果在 BootStrap 中添加了 hibernastore,一切正常

class BootStrap {

    HibernateDatastore hibernateDatastore
    DatabaseProvisioningService databaseProvisioningService

    def init = { servletContext ->
        def databaseConfiguration = databaseProvisioningService.findDatabaseConfiguration("dbName")
        hibernateDatastore.getConnectionSources().addConnectionSource(databaseConfiguration.dataSourceName, databaseConfiguration.configuration)
    }
    def destroy = {
    }
}

但是如果在用户登录时在运行时调用相同的代码

class AuthenticationController
{
  static scope="session"

  HibernateDatastore hibernateDatastore
  AuthenticationService authenticationService
  DatabaseProvisioningService databaseProvisioningService

  def index() {}

  def login()
  {
    def loginId = params.get("loginId")
    def password = params.get("password")
    if (loginId != null && password != null)
    {
      def user = authenticationService.login(loginId, password)
      if(user)
      {
        session.user = user
        session.setAttribute(SessionTenantResolver.ATTRIBUTE, "dbName_" + user.tenant.id)

          if(!hibernateDatastore.getConnectionSources().getConnectionSource("dbName_" + user.tenant.id))
          {
            def databaseConfiguration = databaseProvisioningService.findDatabaseConfiguration("dbName_" + user.tenant.id)
            hibernateDatastore.getConnectionSources().addConnectionSource(databaseConfiguration.dataSourceName, databaseConfiguration.configuration)
          }
      }
    }

    redirect(controller: "user", action: "index")
  }


}

一些方法开始失败时没有实体错误 失败的方法是

User.count()
User.save()

工作方法是

User.findById()
User.findByName()

错误堆栈

java.lang.reflect.InvocationTargetException: null
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)
    at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)
    at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
    at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.IllegalArgumentException: Not an entity: class User
    at org.hibernate.metamodel.internal.MetamodelImpl.entity(MetamodelImpl.java:566)
    at org.hibernate.query.criteria.internal.QueryStructure.from(QueryStructure.java:127)
    at org.hibernate.query.criteria.internal.CriteriaQueryImpl.from(CriteriaQueryImpl.java:158)
    at org.grails.orm.hibernate.AbstractHibernateGormStaticApi$_count_closure4.doCall(AbstractHibernateGormStaticApi.groovy:183)
Caused by: java.lang.IllegalArgumentException: Not an entity: class User

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:297)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:241)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:120)
    at org.grails.orm.hibernate.AbstractHibernateGormStaticApi.count(AbstractHibernateGormStaticApi.groovy:180)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.count(GormEntity.groovy:649)
    at com.ayuka.stobil.ClientService.$mt__getCount(ClientService.groovy:42)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at grails.gorm.multitenancy.Tenants$_withId_closure2$_closure6.doCall(Tenants.groovy:265)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.executeWithExistingOrCreateNewSession(GrailsHibernateTemplate.java:210)
    at org.grails.orm.hibernate.AbstractHibernateDatastore.withNewSession(AbstractHibernateDatastore.java:369)
    at grails.gorm.multitenancy.Tenants$_withId_closure2.doCall(Tenants.groovy:258)
    at grails.gorm.multitenancy.Tenants$CurrentTenant.withTenant(Tenants.groovy:358)
    at grails.gorm.multitenancy.Tenants.withId(Tenants.groovy:236)
    at org.grails.datastore.gorm.services.DefaultTenantService.withCurrent(DefaultTenantService.groovy:71)
    at com.ayuka.stobil.ClientController.$mt__indexJson(ClientController.groovy:50)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at grails.gorm.multitenancy.Tenants$_withId_closure2$_closure6.doCall(Tenants.groovy:265)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:297)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:241)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.executeWithNewSession(GrailsHibernateTemplate.java:153)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.executeWithExistingOrCreateNewSession(GrailsHibernateTemplate.java:207)
    at org.grails.orm.hibernate.AbstractHibernateDatastore.withNewSession(AbstractHibernateDatastore.java:369)
    at grails.gorm.multitenancy.Tenants$_withId_closure2.doCall(Tenants.groovy:258)
    at grails.gorm.multitenancy.Tenants$CurrentTenant.withTenant(Tenants.groovy:358)
    at grails.gorm.multitenancy.Tenants.withId(Tenants.groovy:236)
    at org.grails.datastore.gorm.services.DefaultTenantService.withCurrent(DefaultTenantService.groovy:71)
    ... 13 common frames omitted

注意:在这种情况下正确添加了数据存储,它显示在使用调试模式检查时,但域类功能抛出上述错误

尝试添加代码以在 Bootstrap 类中添加数据存储,一切都开始工作

hibernate grails grails-orm multi-tenant
© www.soinside.com 2019 - 2024. All rights reserved.