首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在grails3中使用mongodb和hibernate

在grails3中使用mongodb和hibernate
EN

Stack Overflow用户
提问于 2016-08-08 13:43:40
回答 3查看 804关注 0票数 3

我需要运行一个针对MySql/Hibernate和MongoDB的Grails 3应用程序。(Had正在Grails2.5上运行,没有任何故障)。

我尝试了许多不同的组合,搜索和尝试相关的答案,但没有运气。

配置使用了最新的Mongodb插件文档。

使用Grails 3.1.10:

代码语言:javascript
复制
grailsVersion=3.1.10

我从build.gradle中的以下细节开始:

代码语言:javascript
复制
buildscript {
  ext {
    grailsVersion = project.grailsVersion
  }
  repositories {
    mavenLocal()
    maven { url "https://repo.grails.org/grails/core" }
  }
  dependencies {
    classpath "org.grails:grails-gradle-plugin:$grailsVersion"
    classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.8.2"
    classpath "org.grails.plugins:hibernate4:5.0.10"
  }
}
...
dependencies {
  compile "org.springframework.boot:spring-boot-starter-logging"
  compile "org.springframework.boot:spring-boot-autoconfigure"
  compile "org.grails:grails-core"
  compile "org.springframework.boot:spring-boot-starter-actuator"
  compile "org.springframework.boot:spring-boot-starter-tomcat"
  compile "org.grails:grails-dependencies"
  compile "org.grails:grails-web-boot"
  compile "org.grails.plugins:cache"
  compile "org.grails.plugins:scaffolding"
  compile "org.grails.plugins:hibernate4"
  compile "org.hibernate:hibernate-ehcache"
  console "org.grails:grails-console"
  profile "org.grails.profiles:web"

  runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.8.2"

  testCompile "org.grails:grails-plugin-testing"
  testCompile "org.grails.plugins:geb"
  testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
  testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"

  //databases
  compile 'org.grails.plugins:mongodb:6.0.0.M2'
  runtime 'mysql:mysql-connector-java:5.1.36'
  ...
 }

application.yml:

代码语言:javascript
复制
---
hibernate:
  cache:
    queries: false
    use_second_level_cache: true
    use_query_cache: false
    region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory

dataSource:
  pooled: true
  jmxExport: true
  driverClassName: com.mysql.jdbc.Driver

environments:
  development:
    dataSource:
        dbCreate: update
        url: jdbc:mysql://localhost:3306/dbname
        driverClassName: com.mysql.jdbc.Driver
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        username : "xxx"
        password : "yyy"
        properties:
            maxActive : 50
            maxIdle: 25
            minIdle: 5
            ...
   grails:
     mongodb:
       host: "localhost"
       port: 27017
       username: "blah"
       password: "blah"
       databaseName: "foo"

这给了我一个错误:

代码语言:javascript
复制
15:13:58.001 [QUIET] [system.out] ERROR     org.springframework.boot.SpringApplication - Application startup failed
15:13:58.001 [QUIET] [system.out] java.lang.NoClassDefFoundError: org/grails/datastore/gorm/plugin/support/ConfigSupport
15:13:58.001 [QUIET] [system.out]   at grails.plugins.mongodb.MongodbGrailsPlugin.doWithSpring(MongodbGrailsPlugin.groovy:42)

MongoDB grails插件找不到ConfigSupport

代码语言:javascript
复制
ConfigSupport.prepareConfig(config, (ConfigurableApplicationContext) applicationContext)

然后,我尝试在所有可能的顺序中添加以下插件:

代码语言:javascript
复制
//compile 'org.grails:grails-datastore-gorm-hibernate4:6.0.0.M2'
//compile 'org.grails:grails-datastore-core:4.0.0.M2'
//compile 'org.grails:grails-datastore-gorm-plugin-support:6.0.0.M2'
//compile 'org.springframework.data:spring-data-mongodb:1.9.2.RELEASE'
//compile 'org.mongodb:mongodb-driver:3.3.0'
//compile 'org.mongodb:mongo-java-driver:3.3.0'

不走运。

我还将mongodb:节移到yml文件中的grails:节。同样的错误。

因此,问题是如何让MongoDB与MySql一起使用grails3运行。

EN

回答 3

Stack Overflow用户

发布于 2016-08-09 08:38:36

好吧愚蠢的我。我使用以下简单的依赖项设置解决了这个难题:

代码语言:javascript
复制
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.grails:grails-core"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.grails.plugins:cache"
compile "org.grails.plugins:scaffolding"
compile "org.grails.plugins:hibernate4"
compile "org.hibernate:hibernate-ehcache"
console "org.grails:grails-console"
profile "org.grails.profiles:web"
runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.8.2"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"

//databases
compile 'org.grails.plugins:mongodb'
runtime 'mysql:mysql-connector-java:5.1.36'

runtime 'com.googlecode.json-simple:json-simple:1.1'

以下几行似乎起到了作用:

代码语言:javascript
复制
compile 'org.grails.plugins:mongodb'

省略版本号。这一点应该在文档中解释得更好一些。通常情况下,定义版本是件好事,或者这是不是太过时了?:-)

很高兴它能起作用

票数 2
EN

Stack Overflow用户

发布于 2016-08-22 14:29:51

好的,我找到了以前正确的答案,用于引用mongodb插件,即

compile 'org.grails.plugins:mongodb'

但我想用一个简短的清单来总结,经过多次试验和错误之后,我有一个正确运行的混合解决方案( hibernate + mysql和mongodb)。

因此,为了具有混合的mysql/mongo grails3配置,您必须

  1. 在org.grails.plugins:mongodb中插入编译‘BuildConfig.groovy’(没有任何版本)(请记住grails 3中没有BuildConfig.groovy,如documented here/grails2->3)
  2. 保持hibernate/mysql的普通依赖关系,例如:compile 'org.grails.plugins:hibernate4' compile 'org.hibernate:hibernate-ehcache' runtime 'mysql:mysql-connector-java:5.1.38'
  3. 在application.yml中正确地设置连接,但我更喜欢在GRAILS/conf中使用旧风格的application.groovy,如下所示(请记住,在GRAILS3中,与2不同,您必须使用mongodb,而不是mongo关键字)
  4. 使用普通GRAILS实体进行mysql映射
  5. 并在mongo实体中使用static mapWith="mongo" (如葡萄-芒果中所述)
  6. 不对mongo使用通常的mysql Long id,而是使用ObjectId id,以及import org.bson.types.ObjectId

Application.groovy设置

代码语言:javascript
复制
grails {   
  mongodb {
        host = "localhost"
        port = 27017
        username = mongouser 
        password= mongopasswd 
        databaseName = mongoDBname
      }
}
environments {
  development {
    dataSource {
      driverClassName = "com.mysql.jdbc.Driver"
      username = ...
      password = ...
      dbCreate = "create"
      url = "jdbc:mysql://127.0.0.1:3306/dbnameMysql"
    }
  }
} 

就这样。享受MongoDB吧。

票数 1
EN

Stack Overflow用户

发布于 2016-08-09 08:59:51

除了显示已确认有效的配置之外,我不确定还能提供更多帮助。最近,我将一个2.4.4应用程序转换为3.1.8,当然,我使用的是Oracle,而不是mysql。

在build.gradle中,我只拥有:

代码语言:javascript
复制
dependencies {
classpath "org.grails.plugins:hibernate4:5.0.6"
...
}

dependencies {
compile "org.grails.plugins:mongodb:5.0.0.RC1"
...
}

在application.yml中:

代码语言:javascript
复制
mongodb:
    databaseName: 'myApp'
    engine: mapping

引擎:映射位对我来说至关重要,它恢复到前面的持久性引擎,如这里所描述的http://gorm.grails.org/latest/mongodb/manual/index.html

我的MongodbGrailsPlugin.groovy中的MongodbGrailsPlugin.groovy()闭包如下所示:

代码语言:javascript
复制
Closure doWithSpring() {
def initializer = new MongoDbDataStoreSpringInitializer(config,     grailsApplication.getArtefacts(DomainClassArtefactHandler.TYPE).collect() {     GrailsClass cls -> cls.clazz })
initializer.registerApplicationIfNotPresent = false
initializer.setSecondaryDatastore( manager.hasGrailsPlugin("hibernate")  )
return     initializer.getBeanDefinitions((BeanDefinitionRegistry)applicationContext)
}

没有对ConfigSupport的引用,它可能已经在您的后期版本中添加(或者没有),或者缺少一个依赖项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38830836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档