<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    瘋狂

    STANDING ON THE SHOULDERS OF GIANTS
    posts - 481, comments - 486, trackbacks - 0, articles - 1
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    hibernate annoation (八 關(guān)聯(lián)映射)

    Posted on 2009-11-02 14:54 瘋狂 閱讀(381) 評(píng)論(0)  編輯  收藏 所屬分類: hibernate

    onetoone:單向

    1,主鍵關(guān)聯(lián):

     在關(guān)聯(lián)放使用@OneToOne

    sql語(yǔ)句:(類代碼見(jiàn)同前面的代碼)

    Java代碼
    1. create table A (id integer not null auto_increment, aname varchar(255), b_id integer, primary key (id))   
    2. create table B (id integer not null auto_increment, bname varchar(255), primary key (id))   
    3. alter table A add index FK41FCD34905 (b_id), add constraint FK41FCD34905 foreign key (b_id) references B (id)  

    可以使用@PrimaryKeyJoinColumn進(jìn)行關(guān)聯(lián)

    2 雙向:

    在關(guān)聯(lián)方使用 

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="b")

    被關(guān)聯(lián)方使用

    @OneToOne(mappedBy="b")

    最終sql:

    Java代碼
    1. create table A (id integer not null auto_increment, aname varchar(255), b integer, primary key (id))   
    2. create table B (id integer not null auto_increment, bname varchar(255), primary key (id))   
    3. alter table A add index FK41FCA54B4F (b), add constraint FK41FCA54B4F foreign key (b) references B (id)  

    如果不寫

    @OneToOne(mappedBy="b")則會(huì)在被關(guān)聯(lián)放也生成一個(gè)字段

    最終代碼:

    Java代碼
    1. create table A (id integer not null auto_increment, aname varchar(255), i_id integer, primary key (id))   
    2. create table B (id integer not null auto_increment, bname varchar(255), a_id integer, primary key (id))   
    3. alter table A add index FK41FCD6779E (i_id), add constraint FK41FCD6779E foreign key (i_id) references B (id)   
    4. alter table B add index FK42FCD2D4A5 (a_id), add constraint FK42FCD2D4A5 foreign key (a_id) references A (id)  

     

     

      如果沒(méi)有寫@JoinColumn(name="b")則默認(rèn)是關(guān)聯(lián)屬性名+下劃線+id

    最終sql:

     

    Java代碼
    1. create table A (id integer not null auto_increment, aname varchar(255), b_id integer, primary key (id))   
    2. create table B (id integer not null auto_increment, bname varchar(255), primary key (id))   
    3. alter table A add index FK41FCD34905 (b_id), add constraint FK41FCD34905 foreign key (b_id) references B (id)  

     可以使用@JoinColumn(referencedColumnName="bname")讓主關(guān)聯(lián)方不關(guān)聯(lián)被關(guān)聯(lián)放的主鍵

    最終sql

    Java代碼 復(fù)制代碼
    1. create table A (id integer not null auto_increment, aname varchar(255), b_bname varchar(255), primary key (id))   
    2. create table B (id integer not null auto_increment, bname varchar(255), primary key (id), unique (bname))   
    3. alter table A add index FK41E47CD6BD (b_bname), add constraint FK41E47CD6BD foreign key (b_bname) references B (bname)  

      3 關(guān)聯(lián)表

     使用

    @OneToOne(cascade=CascadeType.ALL)
     @JoinTable(name="centert",joinColumns=@JoinColumn(name="aid"),inverseJoinColumns=@JoinColumn(name="bid"))

    最終sql:

    寫道
    create table A (id integer not null auto_increment, aname varchar(255), primary key (id))
    create table B (id integer not null auto_increment, bname varchar(255), primary key (id))
    create table centert (bid integer, aid integer not null, primary key (aid))
    alter table centert add index FK27A6BEBFFCA6C7EA (bid), add constraint FK27A6BEBFFCA6C7EA foreign key (bid) references B (id)
    alter table centert add index FK27A6BEBFFCA6C428 (aid), add constraint FK27A6BEBFFCA6C428 foreign key (aid) references A (id)

     

    manytoone

    和onetoone很相似

    特殊情況:果不寫:mappedBy這會(huì)產(chǎn)生中間表:

    Java代碼
    1. create table A (id integer not null auto_increment, aname varchar(255), i_id integer, primary key (id))   
    2. create table B (id integer not null auto_increment, bname varchar(255), primary key (id))   
    3. create table B_A (B_id integer not null, a_id integer not null, unique (a_id))   
    4. alter table A add index FK41FCD6779E (i_id), add constraint FK41FCD6779E foreign key (i_id) references B (id)   
    5. alter table B_A add index FK10384FCD34905 (B_id), add constraint FK10384FCD34905 foreign key (B_id) references B (id)   
    6. alter table B_A add index FK10384FCD2D4A5 (a_id), add constraint FK10384FCD2D4A5 foreign key (a_id) references A (id)  

     

     如

    targetEntity屬性可以關(guān)聯(lián)接口

    例如接口代碼

    Java代碼
    1. public interface I {   
    2.   
    3. }  

     class B implments I

    關(guān)聯(lián)方:

    Java代碼
    1. private I i;   
    2. @ManyToOne(targetEntity=B.class)   
    3.     public I getI() {   
    4.         return i;   
    5.     }  

     

    最終sql:

    Java代碼
    1. create table A (id integer not null auto_increment, aname varchar(255), i_id integer, primary key (id))   
    2. create table B (id integer not null auto_increment, bname varchar(255), primary key (id))   
    3. alter table A add index FK41FCD6779E (i_id), add constraint FK41FCD6779E foreign key (i_id) references B (id)  

     

    主站蜘蛛池模板: 亚洲网址在线观看| 国产成人精品免费视频大| 亚洲国产系列一区二区三区| 亚洲欧洲成人精品香蕉网| 国产精品免费播放| 免费无码A片一区二三区| 91香蕉国产线在线观看免费| 久久WWW免费人成—看片| 亚洲av成人片在线观看| 亚洲人成网男女大片在线播放 | 亚洲Av无码一区二区二三区| 亚洲欧洲∨国产一区二区三区 | 污视频网站在线观看免费| 国产成人亚洲精品| 亚洲精品欧洲精品| 亚洲欧洲免费视频| 亚洲AV一宅男色影视| 国产亚洲精品资在线| 亚洲福利精品一区二区三区| 免费理论片51人人看电影| 在线观看特色大片免费视频| 最近免费中文字幕mv在线电影| 国产免费拔擦拔擦8X高清在线人| 一级女人18片毛片免费视频| 真正全免费视频a毛片| 朝桐光亚洲专区在线中文字幕| 亚洲日韩一中文字暮| 亚洲а∨天堂久久精品9966| 亚洲一区二区三区不卡在线播放| 亚洲精品成人网站在线播放| 亚洲美女免费视频| 亚洲国产成人九九综合| 7777久久亚洲中文字幕| 久久精品国产亚洲av麻豆蜜芽| 亚洲伊人久久大香线蕉影院| 亚洲人成高清在线播放| 精品久久久久久亚洲精品| 亚洲欧洲免费无码| 精品久久久久久亚洲综合网| 老司机午夜在线视频免费观| 爱情岛论坛免费视频|