【问题标题】:grails h2 database create tablegrails h2数据库创建表
【发布时间】:2013-11-21 13:07:27
【问题描述】:

我有一个非常简单的域类

class Person {

  String code

}

而且我也在使用默认的 DataSource 配置:

dataSource {
  pooled = true
  driverClassName = "org.h2.Driver"
  username = "sa"
  password = ""
}
hibernate {
  cache.use_second_level_cache = true
  cache.use_query_cache = false
  cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
  development {
    dataSource {
      dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
      url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
      dialect = "org.hibernate.dialect.H2Dialect"
    }
  } ...

但是当我打开 /dbconsole 时,我看到 DB 是空的并且没有表。为什么?我做错了什么?

【问题讨论】:

    标签: database grails h2


    【解决方案1】:

    当您打开dbconsole 时,您必须确保它读取正确的数据库。

    在您的 url 部分,将 dbconsole 指向您的应用正在使用的 url。在你的情况下:

    jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000
    

    【讨论】:

      【解决方案2】:

      我的项目我正在使用 mysql,我的数据源看起来像

      dataSource {
          pooled = true
          driverClassName = "com.mysql.jdbc.Driver"
          loggingSql = false
          properties {
              validationQuery="select 1"
              testWhileIdle=true
              timeBetweenEvictionRunsMillis=60000
          }
      }
      hibernate {
          cache.use_second_level_cache = false
          cache.use_query_cache = false
          cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
          generate_statistics = false
      }
      // environment specific settings
      environments {
          development {
          dataSource {
              dbCreate = "update" // one of 'create', 'create-drop','update'
              url="jdbc:mysql://localhost:3306/my_db"
              username="XXX"
              password="XXXXXX"
          }
      
          }       
      
      
      }
      

      而且这个配置运行良好

      【讨论】:

        【解决方案3】:

        如果您在开发模式下运行 Grails 并进行更改,我相信如果默认设置到位,它们将不会提交到 H2 数据库。您需要在生产模式下运行应用程序或更改一些配置属性才能在 DBConsole 中查看更新的信息。
        Similar StackOverflow Question

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-08-06
          • 1970-01-01
          • 2013-01-31
          • 2019-01-14
          • 1970-01-01
          • 2021-10-30
          • 1970-01-01
          • 2020-08-23
          相关资源
          最近更新 更多