以一对一关系映射Grails域类 [英] Grails domain-classes mapping in one-to-one relation

查看:93
本文介绍了以一对一关系映射Grails域类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



PostgreSQL中的表:


$ b $我已经准备好了数据库表模式,我需要在我的Grails应用程序中使用它。 b

  CREATE TABLEuser(
id序列NOT NULL,
登录字符变化(32)NOT NULL,
密码字符变化(32 )NOT NULL,
电子邮件字符变化(255)NOT NULL,
date_created带时区的时间NOT NULL DEFAULT now(),
last_updated带时区的时间NOT NULL DEFAULT now(),
is_banned boolean DEFAULT false,
CONSTRAINTPK_user_idPRIMARY KEY(id),
CONSTRAINTUN_user_emailUNIQUE(email),
CONSTRAINTUN_user_loginUNIQUE(登录)


CREATE TABLE profile(
userinteger NOT NULL DEFAULT nextval('profile_id_seq':: regclass),
first_name字符变化(25)NOT NULL,
中间名字符变化(25)NOT NULL,
姓氏字符变化(25)NOT NULL,
地址整数,
CONSTRAINTPK_PROFILE_userPRIMARY KEY(user),
CONSTRAINTFK_PROFILE_user_USER_idFOREIGN KEY(user)
REFERENCESuser(id)MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE CASCADE

正如您所看到的,profile表具有主键,这也是其外键。这是与geails映射有关的主要特性。

我将表映射到grails域类的实现:

  class User {
...
static hasOne = [profile:Profile];
...
}

类配置文件{
...
用户用户;
...
static mapping = {
id name:'user'
version false
address column:'address'
user column:'`用户''
};
...
}

p>

 调用init方法失败;嵌套的异常是org.hibernate.MappingException:无法确定ru.redlisa.model.User的类型,在table:profile,列:[org.hibernate.mapping.Column(user)] 




  1. 如何将表格正确映射到grails域类?


  2. 如何获得交互界面?

  3. p>

      User user = new User(); 
    user.addToProdile(new Profile());



      new User(profile:new Profile())。save(); 


    解决方案

    非常感谢araxn1d用外国发电机显示正确的方式。我重写了这样的域名:

      class User {
    ...
    static hasOne = [profile:Profile];
    ...
    }

    class档案{
    ...
    static belongsTo = [address:Address,
    user:User] ;
    ...
    static mapping = {
    id列:'`user`',generator:'foreign',params:[property:'user']
    version false
    地址栏:'地址'
    用户栏:'`用户'',可插入:false,可更新:false
    };
    ...
    }

    它可以工作!


    I have ready database table schema and I need use it in my Grails app.

    My tables in PostgreSQL:

    CREATE TABLE "user" (
      id serial NOT NULL,
      login character varying(32) NOT NULL,
      password character varying(32) NOT NULL,
      email character varying(255) NOT NULL,
      date_created time with time zone NOT NULL DEFAULT now(),
      last_updated time with time zone NOT NULL DEFAULT now(),
      is_banned boolean DEFAULT false,
      CONSTRAINT "PK_user_id" PRIMARY KEY (id),
      CONSTRAINT "UN_user_email" UNIQUE (email),
      CONSTRAINT "UN_user_login" UNIQUE (login)
    )
    
    CREATE TABLE profile (
      "user" integer NOT NULL DEFAULT nextval('profile_id_seq'::regclass),
      first_name character varying(25) NOT NULL,
      middle_name character varying(25) NOT NULL,
      last_name character varying(25) NOT NULL,
      address integer,
      CONSTRAINT "PK_PROFILE_user" PRIMARY KEY ("user"),
      CONSTRAINT "FK_PROFILE_user_USER_id" FOREIGN KEY ("user")
          REFERENCES "user" (id) MATCH SIMPLE
          ON UPDATE RESTRICT ON DELETE CASCADE
    )
    

    As you can see, "profile" table has primary key, which is its foreign key too. This is main "feature" with which the problems with geails mapping.

    My implementation of tables mapping to grails domain classes:

    class User {
        ...
        static hasOne = [profile : Profile];
        ...
    }
    
    class Profile {
        ...
        User user;
        ...
        static mapping = {
            id name: 'user'
            version false
            address column: 'address'
            user column: '`user`'
        };
        ...
    }
    

    This class mapping crash with exception:

    Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: ru.redlisa.model.User, at table: profile, for columns: [org.hibernate.mapping.Column(user)]
    

    1. How to correctly map the tables to grails domain-classes?

    2. How to get interaction interface?

    like this:

    User user = new User();
    user.addToProdile(new Profile());
    

    Or

    new User(profile: new Profile()).save();
    

    解决方案

    Big thanks to araxn1d for showing right way with foreign generator. I've rewrite my domains like this:

    class User {
        ...
        static hasOne = [profile : Profile];
        ...
    }
    
    class Profile {
        ...
        static belongsTo = [address: Address,
                            user:    User];
        ...
        static mapping = {
            id column: '`user`', generator: 'foreign', params: [ property: 'user']
            version false
            address column: 'address'
            user column: '`user`', insertable: false, updateable: false
        };
        ...
    }
    

    and it works!

    这篇关于以一对一关系映射Grails域类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆