使用milsodb和hibernate with grails3

时间:2016-08-08 13:43:40

标签: mysql mongodb hibernate grails grails-3.1

我需要针对 MySql / Hibernate MongoDB 运行 Grails 3应用。 (如果没有故意在Grails 2.5上运行)。

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

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

使用Grails 3.1.10:

grailsVersion=3.1.10

我从build.gradle中的以下详细信息开始:​​

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:

---
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"

现在这给了我一个错误:

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

ConfigSupport.prepareConfig(config, (ConfigurableApplicationContext) applicationContext)

然后我尝试在所有可能的订单中添加以下插件:

//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 :部分。同样的错误。

所以问题是如何使用grails3使用MySql运行MongoDB。

3 个答案:

答案 0 :(得分:1)

好的,愚蠢的我。我使用这些简单的依赖设置解决了这个难题:

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'

以下行似乎有所不同:

compile 'org.grails.plugins:mongodb'

省略版本号。这应该在文档中更好地解释。通常,定义版本是一件好事,或者这些日子过于陈旧? : - )

很高兴让它发挥作用

答案 1 :(得分:1)

好的,我发现前面提到mongodb插件的答案是正确的,即是compile 'org.grails.plugins:mongodb'
但是我想总结一份简短的清单,让我正确运行混合解决方案(hibernate +经过多次试验和错误后,mysql和mongodb)。
因此,为了拥有混合的mysql / mongo grails3配置,你必须

  1. insert compile' org.grails.plugins:mongodb' (在build.gradle中没有任何版本)(请记住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中正确设置了连接,但我更喜欢旧式的application.groovy,在grails-app / conf中(如旧GRAILS中),如下所示(请记住,在GRAILS3中,与2不同,你必须使用< strong> mongodb ,不再 mongo 关键字)
  4. 使用普通的GRAILS实体进行mysql映射
  5. 并在mongo实体中使用static mapWith="mongo"(如GRAILS-mongo doc中所述)
  6. 不使用mongo的常用mysql Long id,但ObjectId id及其import org.bson.types.ObjectId

  7. Application.groovy设置

    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。

答案 2 :(得分:0)

除了显示已确认正常工作的配置外,我不确定能提供多少帮助。 我最近将2.4.4应用程序转换为3.1.8,不可否认我使用Oracle作为我的RDBMS而不是mysql。

在build.gradle中我只有:

dependencies {
classpath "org.grails.plugins:hibernate4:5.0.6"
...
}

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

在application.yml:

mongodb:
    databaseName: 'myApp'
    engine: mapping

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

MongodbGrailsPlugin.groovy中的doWithSpring()闭包看起来像:

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没有引用,或者缺少相关性。