Hibernate.one-to-one外键策略

[ref]http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.html http://www.hibernate.org/5.html

可用两种方法实现:

1. 主键关联

     [主]:  one-to-one

     [从]:  one-to-one constrained="true"  name="@PP"

              主键ID: generator-class="foreign" generator-param name="property" value="@PP"  

2. 惟一外键关联

    [主]: many-to-one unique="true"  name="@P1"   

    [从]: one-to-one property-ref="@P1" (不需双向可省略该属性)

1. Station 1--1 StationDetail    Station.id作为StationDetail的外键,存储为StationDetail.id.

Station (必须要有getDetail属性)

 /**
  * 修改StationDetail的id生成策略为foreign
  * @hibernate.one-to-one cascade="all"
  */
 public StationDetail getDetail() {
     return detail;
 }

StationDetail (必须要有getStation属性)

 /**
  * @hibernate property="id"
  * @hibernate.id
  * column="id"
  * unsaved-value="-1"
  * generator-class="foreign"
  * @hibernate.generator-param name="property" value="station"  
station为指向外键的属性
  */
 public Long getId() {
  return id;
 } 

     /**
     * constrained="true" 表示使用该属性为主键
     * @hibernate.one-to-one constrained="true" name="station"
     * @return
     */
    public Station getStation() {
        return station;
    }

产生SQL

create table t_station_detail (
   id bigint not null,
   address text,
   email varchar(255),
   fax varchar(255),
   introduce text,
   tel varchar(255),
   primary key (id)
)
create table t_station (
   id bigint not null auto_increment,
   code varchar(10),
   name varchar(255),
   primary key (id)
)
alter table t_station_detail add index FKEADD08E7D1B (id), add constraint FKEADD08E7D1B foreign key (id) references t_station (id)


2. Station 1--1 StationDetail    Station.station_detail存储为StationDetail.id

Station

 /**
  * 修改StationDetail的id生成策略为foreign
  * @hibernate.many-to-one name="detail" column="station_detail" cascade="all" unique="true"
  */
 public StationDetail getDetail() {
     return detail;
 }

StationDetail

id生成用native, 如不需要双向关联,下面可以不需要.

   /**
     * constrained="true" 表示使用该属性为主键
     * @hibernate.one-to-one constrained="false" property-ref="detail"
     * @return
     */
    public Station getStation() {
        return station;
    }

产生SQL

create table t_station_detail (
   id bigint not null auto_increment,
   address text,
   email varchar(255),
   fax varchar(255),
   introduce text,
   tel varchar(255),
   primary key (id)
)

create table t_station (
   id bigint not null auto_increment,
   code varchar(10),
   station_detail bigint unique,
   name varchar(255),
   primary key (id)
)

alter table t_station add index FK4A7DF4695C5811BC (station_detail), add constraint FK4A7DF4695C5811BC foreign key (station_detail) references t_station_detail (id)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值