Grails - 表不会被休眠自动生成

时间:2013-12-14 23:25:46

标签: hibernate grails grails-2.0

我已将spring security core插件集成到我的应用程序中。使用s2-quickstart com.myApplication.secureapp SecAppUser SecAppRole创建后。我的域层中又增加了2个类SecAppRole.groovySecAppUser.groovy。我还加入了BootStrap.groovy

class BootStrap {

  def init = { servletContext ->
    def adminRole = new SecAppRole(authority: 'ROLE_ADMIN').save(flush: true)
    def userRole = new SecAppRole(authority: 'ROLE_USER').save(flush: true)

    def testUser = new SecAppUser(username: 'admin', enabled: true, password: 'admin')
    testUser.save(flush: true)

    SecAppUserSecAppRole.create testUser, adminRole, true

    assert SecAppUser.count() == 1
    assert SecAppRole.count() == 2
    assert SecAppUserSecAppRole.count() == 1
  }

  def destroy = {
  }
}

例如SecAppRole.groovy看起来像这样:

class SecAppRole {

    String authority

    static mapping = {
        cache true
    }

    static constraints = {
        authority blank: false, unique: true
    }
}

但是,在将代码添加到Bootstrap.groovy文件后,我得到了:

|Loading Grails 2.3.4
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
Precompiling AST Transformations ...
src C:\Users\GrailsWorkspace\testApplication\target\work\plugins\postgresql-extensions-0.6.1 C:\Users\GrailsWorkspace\testApplication\target\classes
Done precompiling AST Transformations!
..
|Compiling 3 source files
...................................................
|Running Grails application
Configuring Spring Security Core ...
... finished configuring Spring Security Core
Error |
2013-12-15 00:16:25,835 [localhost-startStop-1] ERROR util.JDBCExceptionReporter  - FEHLER: Relation »sec_app_role« existiert nicht
  Position: 96
Error |
2013-12-15 00:16:25,884 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: could not execute query; SQL [select this_.id as id1_0_, this_.version as version1_0_, this_.authority as authority1_0_ from sec_app_role this_ where this_.authority=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
Message: could not execute query; SQL [select this_.id as id1_0_, this_.version as version1_0_, this_.authority as authority1_0_ from sec_app_role this_ where this_.authority=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
    Line | Method
->>    8 | doCall                           in BootStrap$_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    308 | evaluateEnvironmentSpecificBlock in grails.util.Environment
|    301 | executeForEnvironment . . . . .  in     ''
|    277 | executeForCurrentEnvironment     in     ''
|    334 | innerRun . . . . . . . . . . . . in java.util.concurrent.FutureTask$Sync
|    166 | run                              in java.util.concurrent.FutureTask
|   1110 | runWorker . . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor
|    603 | run                              in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . . . . . . . . . .  in java.lang.Thread
Caused by SQLGrammarException: could not execute query
->>    8 | doCall                           in BootStrap$_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    308 | evaluateEnvironmentSpecificBlock in grails.util.Environment
|    301 | executeForEnvironment . . . . .  in     ''
|    277 | executeForCurrentEnvironment     in     ''
|    334 | innerRun . . . . . . . . . . . . in java.util.concurrent.FutureTask$Sync
|    166 | run                              in java.util.concurrent.FutureTask
|   1110 | runWorker . . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor
|    603 | run                              in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . . . . . . . . . .  in java.lang.Thread
Caused by PSQLException: FEHLER: Relation »sec_app_role« existiert nicht
  Position: 96
->> 2161 | receiveErrorResponse             in org.postgresql.core.v3.QueryExecutorImpl
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1890 | processResults                   in     ''
|    255 | execute . . . . . . . . . . . .  in     ''
|    560 | execute                          in org.postgresql.jdbc2.AbstractJdbc2Statement
|    417 | executeWithFlags . . . . . . . . in     ''
|    302 | executeQuery                     in     ''
|      8 | doCall . . . . . . . . . . . . . in BootStrap$_closure1
|    308 | evaluateEnvironmentSpecificBlock in grails.util.Environment
|    301 | executeForEnvironment . . . . .  in     ''
|    277 | executeForCurrentEnvironment     in     ''
|    334 | innerRun . . . . . . . . . . . . in java.util.concurrent.FutureTask$Sync
|    166 | run                              in java.util.concurrent.FutureTask
|   1110 | runWorker . . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor
|    603 | run                              in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . . . . . . . . . .  in java.lang.Thread
Error |
Forked Grails VM exited with error

我可以在我的postgresql db中清楚地看到表中没有创建的表。但是,我试过了:

  1. 我使用 grails create-hibernate-cfg-xml创建了hibernate文件。但是这不会改变我的输出。
  2. 我的问题显然是在运行应用程序后没有创建表。如何指定grails 2自动生成表格,例如hibernate

    感谢您的回答!

    更新

    这是我的DataSource.groovy

    dataSource {
        pooled = true
        driverClassName = "org.postgresql.Driver"
        dialect = org.hibernate.dialect.PostgreSQLDialect
        username = "testApplicationUser"
        password = "admin"
    }
    hibernate {
        cache.use_second_level_cache = true
        cache.use_query_cache = false
        cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
        //    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
    }
    
    // environment specific settings
    environments {
        development {
            dataSource {
                dbCreate = ""
                driverClassName = "org.postgresql.Driver"
                dialect = "net.kaleidos.hibernate.PostgresqlExtensionsDialect"
                url = "jdbc:postgresql://localhost:5432/testApplication"
                username = "testApplicationUser"
                password = "admin"
            }
        }
        test {
            dataSource {
                dbCreate = ""
                driverClassName = "org.postgresql.Driver"
                dialect = "net.kaleidos.hibernate.PostgresqlExtensionsDialect"
                url = "jdbc:postgresql://localhost:5432/testApplication"
                username = "testApplicationUser"
                password = "admin"    }
        }
        production {
            dataSource {
                dbCreate = "update"
                url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
                properties {
                    maxActive = -1
                    minEvictableIdleTimeMillis=1800000
                    timeBetweenEvictionRunsMillis=1800000
                    numTestsPerEvictionRun=3
                    testOnBorrow=true
                    testWhileIdle=true
                    testOnReturn=false
                    validationQuery="SELECT 1"
                    jdbcInterceptors="ConnectionState"
                }
            }
        }
    }
    

1 个答案:

答案 0 :(得分:3)

我建议在grails-app / conf / Datasource.groovy中查看dbCreate的值,并确保将其设置为“create”或“update”。有关在Grails中配置数据源的更多信息,请参阅documentation