【问题标题】:Grails dbCreate='validate' fails to recognize existing tableGrails dbCreate='validate' 无法识别现有表
【发布时间】:2012-08-04 18:40:15
【问题描述】:

我的dataSource配置如下:

development {
    dataSource {
        dbCreate = "validate"
        url = "jdbc:sqlserver://myservername"
    }
}

我的 SQL Server“myservername”有一个数据库“products”,其中的一个表“inventory”归“dbo”所有。这是我的 Inventory 类映射:

static mapping = {
    table 'products.dbo.inventory'
    version false
    columns {
        id column: 'itemKey'
        itemId column: 'itemId'
        inactive column: 'inactive'
    }
}

当我尝试运行应用程序时,我收到此错误:

嵌套异常是 org.hibernate.HibernateException: Missing table: products.dbo.inventory

此外,当我将 dbCreate 从“验证”更改为“更新”或任何其他时,它工作正常。

有什么想法吗?

提前致谢,
斯库德

编辑以回答问题 -

我将更改合并到我的对象中,但仍然出现错误。这是我的对象:

class Inventory {

Integer id
String itemId
String description
Boolean inactive

static mapping = {
    table 'products.dbo.inventory'
    version false
    columns {
        id column: 'itemKey'
        itemId column: 'itemId'
        description column: 'description'
        inactive column: 'inactive'
    }
}

static constraints = {
    itemId (maxSize: 30)
    description (maxSize: 100)
}

static hasMany = [productInventoryDefaults: ProductInventoryDefaults, productTreeNodes: ProductTreeNodes]

String toString() {
    return this.itemId
}

}

和桌子:

CREATE TABLE [dbo].[inventory](
[itemKey] [int] NOT NULL,
[itemId] [varchar](30) NOT NULL,
[description] [varchar](100) NOT NULL,
[inactive] [bit] NOT NULL,
 CONSTRAINT [PK_Inventory] PRIMARY KEY CLUSTERED 
(
[itemKey] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

【问题讨论】:

  • Grails 实际上不会更改该表中的任何内容,也许是字段类型?域对象和表结构是什么?
  • 你是说如果在“更新”模式下,Grails 试图改变一些东西(即使它失败了,因为它没有对 db 的 DDL 权限),那么它不会识别表的存在“验证”模式?因为对象和数据库表的列类型存在一些差异。
  • @VictorSergienko,我已经在我的原始帖子中包含了我的对象和表格。
  • 没错。不是试图改变它,而是"validate" 意味着 Hibernate 将验证该表是否与 Hibernate 关于表的“想法”完全匹配:D

标签: sql-server grails datasource grails-orm


【解决方案1】:

我相信这里Hibernate没有办法看到如何生成主键。

我会尝试:

  • id 字段更改为IDENTITY
  • 或/和调整id field mapping 以使用Java 端generator
  • 或者,甚至更简单,尝试使用 dbCreate = "create" 和适当的权限连接到测试数据库,查看 Hibernate 将创建什么表结构,并将您的表更改为那个结构。

如果需要保持表结构不变,那么只有第二种方式了。

【讨论】:

  • 谢谢美女!我会尽力让你知道我的想法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 1970-01-01
  • 2018-01-29
相关资源
最近更新 更多