Carol.Kar
Carol.Kar

Reputation: 5355

Grails - Tables do not get autogenerated by hibernate

I have integrated the spring security core plugin in my application. After creating with s2-quickstart com.myApplication.secureapp SecAppUser SecAppRole. I have gotten 2 more classes SecAppRole.groovy and SecAppUser.groovy in my domain layer. I have also added into my 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 = {
  }
}

For example SecAppRole.groovy looks like that:

class SecAppRole {

    String authority

    static mapping = {
        cache true
    }

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

However, after adding the code to the Bootstrap.groovy file I get:

|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

I can clearly see in my postgresql db that the tables where not created. However, what I tried:

  1. I created the hibernate file with grails create-hibernate-cfg-xml. However this does not change my output.

My Problem clearly is that the tables do not get created after running the application. How to specify that the tables get autogenerated by grails 2, for example with hibernate?

I appreciate your answer!

UPDATE

Here is my 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"
            }
        }
    }
}

Upvotes: 2

Views: 529

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

I would recommend looking at the value of dbCreate in your grails-app/conf/Datasource.groovy and ensure it's set to "create" or "update". Further information about configuring your datasource in Grails can be found in the documentation.

Upvotes: 3

Related Questions