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

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

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

    Duran's technical life
    踏踏實實學技術,認認真真做研究。

    Hibernate一對一關聯實用介紹

    #0
    書和文檔上寫的都不是特清楚的。自己記下來。

    #1 Using a PK association

    #1.1 POJO with XDolclet annotation
    public class Customer {
     /**
      * @return Returns the shoppingCart.
      * @hibernate.many-to-one cascade="delete" column="shopping_cart_id"
      *  unique="true" foreign-key="FK_SHOPPING_CART__CUSTOMER"
      */
     public ShoppingCart getShoppingCart() {
      return shoppingCart;
     }
    }

    public class ShoppingCart {
     /**
      * @return Returns the customer.
      * @hibernate.one-to-one property-ref="shoppingCart"
      */
     public Customer getCustomer() {
      return customer;
     }
    }

    property-ref="shoppingCart" 告訴Hibernate ShoppingCart#customer和Customer#shoppingCart是反向的關系。所以Hibernate知道如何從ShoppingCart#getCustomer中檢索到相應的customer對象。取出某個Customer對象時,Hibernate會生成DEBUG SQL:324 - 3中的SQL語句。

    #1.2 HBM
    Customer.hbm.xml
    <many-to-one
        name="shoppingCart"
        class="ShoppingCart"
        cascade="delete"
        outer-join="auto"
        foreign-key="FK_SHOPPING_CART__CUSTOMER"
        column="shopping_cart_id"
    />

    ShoppingCart.hbm.xml
    <one-to-one
        name="customer"
        class="Customer"
        cascade="none"
        outer-join="auto"
        constrained="false"
        property-ref="shoppingCart"
    />

    #1.3 SCHEMA SQL
    create table CUSTOMER (
        ID bigint generated by default as identity (start with 1),
        SHOPPING_CART_ID bigint,
        primary key (ID)
    )

    create table SHOPPING_CART (
        ID bigint generated by default as identity (start with 1)
        primary key (ID)
    )

    alter table CUSTOMER
        add constraint FK_SHOPPING_CART__CUSTOMER
        foreign key (SHOPPING_CART_ID)
        references SHOPPING_CART

    #1.4 Query SQL
    DEBUG SQL:324 - 1
    select customer0_.ID as ID, customer0_.SHOPPING_CART_ID as SHOPPING2_3_, customer0_.USERNAME as USERNAME3_, customer0_.PWD as PWD3_
    from CUSTOMER customer0_
    where customer0_.USERNAME=? and customer0_.PWD=?

    DEBUG SQL:324 - 2
    select shoppingca0_.ID as ID0_, shoppingca0_.TOTAL as TOTAL8_0_
    from SHOPPING_CART shoppingca0_
    where shoppingca0_.ID=?

    DEBUG SQL:324 - 3
    select customer0_.ID as ID1_, customer0_.SHOPPING_CART_ID as SHOPPING2_3_1_, customer0_.USERNAME as USERNAME3_1_, customer0_.PWD as PWD3_1_, shoppingca1_.ID as ID0_, shoppingca1_.TOTAL as TOTAL8_0_
    from
     CUSTOMER customer0_
     left outer join
     SHOPPING_CART shoppingca1_
     on customer0_.SHOPPING_CART_ID=shoppingca1_.ID
    where customer0_.SHOPPING_CART_ID=?


    #2 Using a FK association

    #2.1 POJO with XDolclet annotation
    public class Customer {
     /**
      * @return Returns the shoppingCart.
      * @hibernate.one-to-one cascade="delete"
      */
     public ShoppingCart getShoppingCart() {
      return shoppingCart;
     }
    }

    public class ShoppingCart {
     /**
      * @return Returns the id.
      * @hibernate.id generator-class="foreign"
      * @hibernate.generator-param name="property" value="customer"
      */
     public Long getId() {
      return id();
     }

     /**
      * @return Returns the customer.
      * @hibernate.one-to-one constrained="true" foreign-key="FK_CUSTOMER__SHOPPING_CART"
      */
     public Customer getCustomer() {
      return customer;
     }
    }

    constrained="true" 告訴Hibernate ShoppingCart的PK還應該是一個FK,這個FK引用Customer的PK。還需要多做一點工作,聲明ShoppingCart的PK生成策略是foreign,對應ShoppingCart#customer。這和上面一句話不是一個意思嘛,FT~~

    #2.2 HBM
    Customer.hbm.xml
    <one-to-one
        name="shoppingCart"
        class="ShoppingCart"
        cascade="delete"
        outer-join="auto"
        constrained="false"
    />

    ShoppingCart.hbm.xml
    <id
        name="id"
        column="id"
        type="java.lang.Long"
    >
        <generator class="foreign">
     <param name="property">customer</param>
        </generator>
    </id>

    <one-to-one
        name="customer"
        class="Customer"
        cascade="none"
        outer-join="auto"
        constrained="true"
    />

    #2.3 SCHEMA SQL
    create table CUSTOMER (
        ID bigint generated by default as identity (start with 1),
        primary key (ID)
    )

    create table SHOPPING_CART (
        ID bigint not null,
        TOTAL integer,
        primary key (ID)
    )

    alter table SHOPPING_CART
        add constraint FK_CUSTOMER__SHOPPING_CART
        foreign key (ID)
        references CUSTOMER

    #2.4 Query SQL
    DEBUG SQL:324 -
    select customer0_.ID as ID, customer0_.USERNAME as USERNAME3_, customer0_.PWD as PWD3_
    from CUSTOMER customer0_
    where customer0_.USERNAME=? and customer0_.PWD=?

    DEBUG SQL:324 -
    select shoppingca0_.ID as ID0_, shoppingca0_.TOTAL as TOTAL8_0_
    from SHOPPING_CART shoppingca0_
    where shoppingca0_.ID=?

    這個“真正”的one-to-one的好處是少條關聯SQL語句,看到了嗎?

    posted on 2005-09-06 13:16 Duran's technical life 閱讀(4194) 評論(2)  編輯  收藏 所屬分類: 技術積累
    Comments
    • # re: Hibernate一對一關聯實用介紹
      liu
      Posted @ 2005-12-27 16:23
      有點亂,能不能加個標題?  回復  更多評論   
    • # re: Hibernate一對一關聯實用介紹
      Jun Tsai
      Posted @ 2006-01-04 15:26
      對HIbernate3.1好像不行吧  回復  更多評論   
     
    主站蜘蛛池模板: 成年女人男人免费视频播放| 免费视频精品一区二区三区| 成人免费在线观看网站| 亚洲天天做日日做天天看| 99精品视频在线观看免费专区 | 中文字幕亚洲免费无线观看日本 | 免费一级毛片在线观看| 日本在线观看免费高清| 久久亚洲国产精品123区| 国产免费一区二区三区免费视频 | 日韩中文字幕在线免费观看| 亚洲日本va一区二区三区| 国产成人免费全部网站 | 亚洲熟女综合色一区二区三区| 无码永久免费AV网站| 亚洲日本VA中文字幕久久道具| 国产成人精品男人免费| eeuss影院免费直达入口| 亚洲av无码专区在线播放| 91短视频在线免费观看| 亚洲午夜精品久久久久久app| 亚洲国产一区视频| 日本在线看片免费人成视频1000| 天天爽亚洲中文字幕| 亚洲AⅤ优女AV综合久久久| 波多野结衣免费一区视频 | 国产成人亚洲综合一区| 免费人成网站在线播放| 免费看黄的成人APP| 亚洲最大成人网色香蕉| 男人的天堂亚洲一区二区三区| 夜夜爽妓女8888视频免费观看 | 久久久久精品国产亚洲AV无码| 免费国产怡红院在线观看| 中文字幕乱码一区二区免费| 久久精品国产亚洲αv忘忧草| 亚洲AⅤ永久无码精品AA| 91短视频在线免费观看| 永久免费精品影视网站| 亚洲在成人网在线看| 亚洲高清视频一视频二视频三|