Grails 3.3.0 M1和3.3.0 M2组合MongoDB和Hibernate失败

时间:2017-06-08 03:10:23

标签: mongodb hibernate grails

Step1:使用命令create app

grails create-app test

第2步:创建域

grails create-domain-class test

Step3:configue build.gradle

compile 'org.grails.plugins:mongodb'

Step4:configue 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:在bootstrap中进行查询测试

println Test.findAll()

Stop7:运行grails app

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有什么变化?

1 个答案:

答案 0 :(得分:0)

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