【问题标题】:Grails 3.3.0 M1 and 3.3.0 M2 Combining MongoDB and Hibernate failGrails 3.3.0 M1 和 3.3.0 M2 结合 MongoDB 和 Hibernate 失败
【发布时间】:2017-11-09 14:16:53
【问题描述】:

Step1:使用命令create app

grails create-app test

第二步:创建域

grails create-domain-class test

Step3:配置build.gradle

compile 'org.grails.plugins:mongodb'

Step4:配置application.yml

grails:
   mongodb:
       host: "localhost"
       port: 27017
       username: ""
       password: ""
       databaseName: "test"
environments:
development:
    dataSource:
        dbCreate: none
        url:jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
        logSql: true

Step5:在Test.groovy中使用mapWith

static mapWith="mongo"

Step6:在引导中查询测试

println Test.findAll()

Stop7:运行 grails 应用程序

grails run-app

错误是:

 org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not prepare statement; bad SQL grammar [select this_.id as id1_0_0_, this_.version as version2_0_0_ from test this_]; nested exception is org.h2.jdbc.JdbcSQLException: Table "TEST" not found; SQL statement:
select this_.id as id1_0_0_, this_.version as version2_0_0_ from test this_ [42102-194]
        at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
        at org.grails.orm.hibernate.GrailsHibernateTemplate.convertJdbcAccessException(GrailsHibernateTemplate.java:723)
        at org.grails.orm.hibernate.GrailsHibernateTemplate.convertHibernateAccessException(GrailsHibernateTemplate.java:711)
        at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:302)
        at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:242)
        at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:116)
        at org.grails.orm.hibernate.HibernateGormStaticApi.list(HibernateGormStaticApi.groovy:74)
        at org.grails.datastore.gorm.GormStaticApi.findAll(GormStaticApi.groovy:579)
        at org.grails.datastore.gorm.GormEntity$Trait$Helper.findAll(GormEntity.groovy:671)
        at org.grails.datastore.gorm.GormEntity$Trait$Helper$findAll$0.call(Unknown Source)

使用其他版本:

  • 3.2.3就可以了
  • 3.2.5就可以了
  • 3.2.9 没问题

我的代码是: the demo in github

3.3.0.M1 和 3.3.0.M2 有什么变化?

【问题讨论】:

  • 显示您的完整 application.yml 代码
  • 你可以看到它:application.yml
  • 3.3.0之前,为域类创建DefaultGrailsDomainClass时,会设置mappingStrategy使用mapWith字段,现在不这样做了,是bug吗?
  • 在我看来是个错误
  • 我为你创建了一个问题github.com/grails/grails-data-mapping/issues/957

标签: mongodb hibernate grails


【解决方案1】:
grails:
    mongodb:
       host: "localhost"
       port: 27017
       username: ""
       password: ""
       databaseName: "test"

environments:
    development:
        dataSource:
            dbCreate: none
            url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
            logSql: true
    test:
        dataSource:
            dbCreate: update
            url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    production:
        dataSource:
            dbCreate: none
            url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
            properties:
                jmxEnabled: true
                initialSize: 5
                maxActive: 50
                minIdle: 5
                maxIdle: 25
                maxWait: 10000
                maxAge: 600000
                timeBetweenEvictionRunsMillis: 5000
                minEvictableIdleTimeMillis: 60000
                validationQuery: SELECT 1
                validationQueryTimeout: 3
                validationInterval: 15000
                testOnBorrow: true
                testWhileIdle: true
                testOnReturn: false
                jdbcInterceptors: ConnectionState
                defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

将您的这些行替换为此

environments:
    development:
        grails:
            mongodb:
                host: "localhost"
                port: 27017
                username: ""
                password: ""
                databaseName: "test"
    test:
        mongodb:
                host: "localhost"
                port: 27017
                username: ""
                password: ""
                databaseName: "test"
    production:
        grails:
            mongodb:
                host: "localhost"
                port: 27017
                username: ""
                password: ""
                databaseName: "test"
            properties:
                jmxEnabled: true
                initialSize: 5
                maxActive: 50
                minIdle: 5
                maxIdle: 25
                maxWait: 10000
                maxAge: 600000
                timeBetweenEvictionRunsMillis: 5000
                minEvictableIdleTimeMillis: 60000
                validationQuery: SELECT 1
                validationQueryTimeout: 3
                validationInterval: 15000
                testOnBorrow: true
                testWhileIdle: true
                testOnReturn: false
                jdbcInterceptors: ConnectionState
                defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

【讨论】:

  • 如果我只想使用 mongo,只需从 build.gradle 中删除 compile "org.grails.plugins:hibernate5",但在我的项目中,我需要结合 MongoDB 和 Hibernate
猜你喜欢
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多