gpt4 book ai didi

hibernate - Grails- hibernate 不会自动生成表

转载 作者:行者123 更新时间:2023-12-02 13:56:40 24 4
gpt4 key购买 nike

我已经将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数据库中清楚地看到未创建表的地方。但是,我尝试了什么:
  • 我使用
    grails create-hibernate-cfg-xml
    创建了休眠文件。但是,这不会改变我的输出。

  • 我的问题很明显是在运行应用程序后无法创建表。如何指定表由 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"
    }
    }
    }
    }

    最佳答案

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

    关于hibernate - Grails- hibernate 不会自动生成表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20589434/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com