postgresql - 连接到 PostgreSQL 的 Grails 应用程序的奇怪行为

标签 postgresql grails grails-orm

我正在尝试将我的 grails 应用程序从 h2 切换到 PostgreSQL。 我为实现目标所采取的步骤:

  1. http://jdbc.postgresql.org/download.html 下载 JDBC (JDBC4 Postgresql 驱动程序,版本 9.3-1100)
  2. 将 JDBC 附加到/lib 文件夹
  3. 更改数据源。现在看起来像:

    dataSource {
        pooled = true
        driverClassName = "org.postgresql.Driver"
        dialect="org.hibernate.dialect.PostgreSQLDialect"
        username = "postgres"
        password = "admin"
    }
    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 = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
                //url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
                url = "jdbc:postgresql://localhost:5432/admin_panel"
            }
        }
        test {
            dataSource {
                dbCreate = "update"
                url = "jdbc:postgresql://localhost:5432/admin_panel"
            }
        }
        production {
            dataSource {
                dbCreate = "update"
                url = "jdbc:postgresql://localhost:5432/admin_panel"
                pooled = true
                properties {
                   maxActive = -1
                   minEvictableIdleTimeMillis=1800000
                   timeBetweenEvictionRunsMillis=1800000
                   numTestsPerEvictionRun=3
                   testOnBorrow=true
                   testWhileIdle=true
                   testOnReturn=true
                   validationQuery="SELECT 1"
                }
            }
        }
    }
    

现在游戏开始了。我在 GGTS 中输入“run-app”,但出现错误。我尝试使用 BootStrap 创建的对象无法初始化,因为验证: 初始化应用程序时出错:save() 期间发生验证错误 。 这真的很奇怪,因为该消息说对先前创建的对象的引用为空:字段“subpage”上的对象“adminpanel.component.Text”中的字段错误:拒绝值[null];。 这一行中的“subpage”不可能为空,所以我去pgAdmin III检查是否创建了这条记录,我注意到根本没有创建任何表。

如果应用程序连接到 H2,Everetyhing 就可以工作,但当我将其切换到 postgres 时,Everetyhing 就开始崩溃了。此外,当我从 BootStrap 中删除所有内容时,应用程序启动,我可以正常创建对象,但我仍然无法在 pgAdmin 中看到它们。您对我还可以检查什么或者当我使用 PostgreSQL 时 GORM 不在我的应用程序中创建表有什么建议吗?

提前致谢。


编辑:

经过几次测试,我找到了问题的根源...... PostgreSQL 为每个表中的“id”列提供了一个奇怪的值。当我使用 H2 时,每个表中都有 1..x 的值,在 PostgreSQL 中我有这样的内容:

table1
id:
1
2
3
-
7
8
9

table2
id:
4
5
6
-
10
11

正如您可能注意到的那样,不同表中的所有行的值是可以互换的,所以我不能有例如id 为 1 的对象 table1 和 id 为 1 的对象 table2。你知道为什么吗?

最佳答案

Grails/Hibernate 使用 Sequence 作为 Postgres(或 Oracle 等)等数据库的对象 ID。默认情况下,Grails 使用共享序列 (hibernate_sequence)。因此,所有对象都将具有 uniq id,但每个数据库都是唯一的,而不是每个表。

您可以将域配置为对域使用不同的序列,例如:

static mapping = {
    id generator: 'sequence', params: [sequence: 'my_own_sequence']
}

另请参阅

关于postgresql - 连接到 PostgreSQL 的 Grails 应用程序的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21639863/

相关文章:

jquery - 如何将JSON渲染为jQuery

grails - Grails在gsp中比较2个变量

mysql - GORM tablePerHierarchy false,外键

grails - 保存域对象失败时默认 o.errors.allErrors.each { println it }

mysql - 从 PostgreSQL 翻译到 MariaDB 的这个查询有什么问题?

sql - PostgreSQL 触发器在使用外键插入表 A 后插入表 B

java - 使用 java 在终端中读取 postgres 表

hibernate - 如何定义域中是否有多个唯一字段?

Grails 单向多对多

ruby-on-rails - 部署 Rails 应用程序时为 "PG::UndefinedColumn:"