<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 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    hibernate annoation (八 關聯映射)

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

    onetoone:單向

    1,主鍵關聯:

     在關聯放使用@OneToOne

    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)  

    可以使用@PrimaryKeyJoinColumn進行關聯

    2 雙向:

    在關聯方使用 

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

    被關聯方使用

    @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")則會在被關聯放也生成一個字段

    最終代碼:

    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)  

     

     

      如果沒有寫@JoinColumn(name="b")則默認是關聯屬性名+下劃線+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")讓主關聯方不關聯被關聯放的主鍵

    最終sql

    Java代碼 復制代碼
    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 關聯表

     使用

    @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這會產生中間表:

    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屬性可以關聯接口

    例如接口代碼

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

     class B implments I

    關聯方:

    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)  

     

    主站蜘蛛池模板: 亚洲午夜精品一区二区| 亚洲人成精品久久久久| 亚洲va精品中文字幕| 一区二区三区观看免费中文视频在线播放 | 精品久久久久久亚洲| 国产一级高青免费| 亚洲av色福利天堂| 亚洲最大免费视频网| 亚洲综合一区无码精品| 日本午夜免费福利视频| 黄页网址在线免费观看| 中文国产成人精品久久亚洲精品AⅤ无码精品| 一级黄色免费大片| 亚洲成AV人在线播放无码| 麻豆成人久久精品二区三区免费| 91久久亚洲国产成人精品性色| 最近免费中文字幕高清大全 | 日韩一级片免费观看| 久久久久亚洲AV综合波多野结衣| 国产美女视频免费观看的网站| 亚洲V无码一区二区三区四区观看 亚洲αv久久久噜噜噜噜噜 | 99免费在线视频| 亚洲国产高清在线精品一区| 夜夜嘿视频免费看| 一级人做人爰a全过程免费视频| 国产亚洲色视频在线| 亚洲视频免费观看| 亚洲国产精品成人AV在线| 亚洲午夜激情视频| 国产精品1024永久免费视频| 亚洲欧美在线x视频| 亚洲成AV人片在| 成人免费视频软件网站| 久久国产乱子伦精品免费午夜| 亚洲美女视频一区| 免费无遮挡无码永久在线观看视频| 91在线免费观看| 亚洲Av永久无码精品黑人| 亚洲Av综合色区无码专区桃色| 女人18毛片a级毛片免费视频| 久久久WWW成人免费精品|