【问题标题】:GORM properties and Postgres fields is domain data typeGORM 属性和 Postgres 字段是域数据类型
【发布时间】:2013-07-11 02:16:10
【问题描述】:

我只是想知道是否可以将 GORM 属性映射到 Postgres 表中数据类型为 postgres 域的字段。验证时出现错误。 谢谢

领域类:

class Userprofile {

long iduser
String username

static mapping = {
    datasource 'ALL'
    id name: 'iduser'
    version false
}
}

postgres 中的表格:

security.userprofile
(

iduser "Primary Key Id" NOT NULL DEFAULT nextval('security.userprofile_seq'::regclass),

username "General Name" NOT NULL,

)

CREATE DOMAIN "Primary Key Id" AS bigint;

CREATE DOMAIN "General Name" AS character varying(100) COLLATE pg_catalog."default";

我已在数据源中将 dbCreate 设置为“验证”,并且在验证 db 时收到此错误消息:

由 HibernateException 引起:用户配置文件中列 iduser 的列类型错误。找到:主要,预期:int8

【问题讨论】:

  • 对此不确定,但我认为如果您在域类中将 long iduser 更改为 Integer iduser(将数据类型更改为整数),那么它应该可以验证。
  • 我尝试将其更改为整数,但没有成功。
  • 你的数据库中是否有现有的表,并且你必须将你的应用程序与它连接起来?,因为我试图用 iduser: bigint-> default: nextval('user_profile_check_iduser_seq': :regclass),并且使用相同的域对象我能够验证它:)
  • 是的,如果我没有在数据源中将 dbCreate 设置为“验证”,我可以将我的应用程序连接到数据库并检索数据。我只是把它留空。这是我的表的样子: CREATE TABLE security.userprofile ( iduserprofile "Primary Key Id" NOT NULL DEFAULT nextval('security.userprofile_seq'::regclass), username "General Name" NOT NULL, password "General Name" NOT NULL , CONSTRAINT userprofile_pkey PRIMARY KEY (iduserprofile) ) WITH (OIDS=FALSE);

标签: java grails jpa jdbc grails-orm


【解决方案1】:

我对 GORM 验证并不完全熟悉。可能必须设置一些额外的逻辑来映射验证规则。然而,关于你的错误的一件事立刻让我想到了。

由 HibernateException 引起:用户配置文件中列 iduser 的列类型错误。找到:主要,预期:int8

现在,您的域甚至不是“主”,而是“主键 ID”。所以 GORM 不仅没有映射它,甚至没有找到正确的标签。

我要做的第一件事是更改您的命名约定,使其全部为小写字母和_。域变为 primary_key_id 和 general_name。看看这能让你走多远,然后探索你还需要做什么才能让 GORM 处理它。

【讨论】:

    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2011-01-09
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多