2013-12-14 151 views
2

我在應用程序中集成了spring security core插件。用s2-quickstart com.myApplication.secureapp SecAppUser SecAppRole創建後。我已經在我的域圖層中獲得了另外兩個類SecAppRole.groovySecAppUser.groovy。我也加入到我的BootStrap.groovyGrails - 休眠表不會自動生成

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數據庫中沒有創建的表。然而,我試過的:

  1. 我創建了hibernate文件 grails create-hibernate-cfg-xml。但是這不會改變我的輸出。

我的問題很明顯是在運行應用程序後表格沒有被創建。如何指定表格由grails 2自動生成,例如hibernate

我很感謝您的回答!

UPDATE

這裏是我的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" 
      } 
     } 
    } 
} 

回答

3

我會建議看dbCreate的在你的grails-app/conf目錄/ DataSource.groovy中的價值,並確保它被設置爲「創建「或」更新「。有關在Grails中配置數據源的更多信息,請參閱documentation

+0

非常感謝您的回答!請參閱我的更新;) – mrquad

+2

非常歡迎。你可以看到你的dbCreate在開發和測試中都是空白的。 (: –

+0

非常感謝您的回答!它現在可以工作;) – mrquad