Duran's technical life
          踏踏實(shí)實(shí)學(xué)技術(shù),認(rèn)認(rèn)真真做研究。

          Hibernate一對(duì)一關(guān)聯(lián)實(shí)用介紹

          #0
          書(shū)和文檔上寫(xiě)的都不是特清楚的。自己記下來(lái)。

          #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是反向的關(guān)系。所以Hibernate知道如何從ShoppingCart#getCustomer中檢索到相應(yīng)的customer對(duì)象。取出某個(gè)Customer對(duì)象時(shí),Hibernate會(huì)生成DEBUG SQL:324 - 3中的SQL語(yǔ)句。

          #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還應(yīng)該是一個(gè)FK,這個(gè)FK引用Customer的PK。還需要多做一點(diǎn)工作,聲明ShoppingCart的PK生成策略是foreign,對(duì)應(yīng)ShoppingCart#customer。這和上面一句話不是一個(gè)意思嘛,F(xiàn)T~~

          #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=?

          這個(gè)“真正”的one-to-one的好處是少條關(guān)聯(lián)SQL語(yǔ)句,看到了嗎?

          posted on 2005-09-06 13:16 Duran's technical life 閱讀(4204) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): 技術(shù)積累
          Comments
          • # re: Hibernate一對(duì)一關(guān)聯(lián)實(shí)用介紹
            liu
            Posted @ 2005-12-27 16:23
            有點(diǎn)亂,能不能加個(gè)標(biāo)題?  回復(fù)  更多評(píng)論   
          • # re: Hibernate一對(duì)一關(guān)聯(lián)實(shí)用介紹
            Jun Tsai
            Posted @ 2006-01-04 15:26
            對(duì)HIbernate3.1好像不行吧  回復(fù)  更多評(píng)論   
           
          主站蜘蛛池模板: 镇康县| 大厂| 平昌县| 洛扎县| 武义县| 炉霍县| 黄山市| 什邡市| 巴中市| 襄汾县| 丹东市| 栖霞市| 广东省| 涿州市| 陆河县| 甘孜县| 新泰市| 正阳县| 谷城县| 利辛县| 阳原县| 平陆县| 班玛县| 江永县| 平舆县| 浦江县| 司法| 永仁县| 福安市| 鄱阳县| 高陵县| 连江县| 纳雍县| 吉木乃县| 筠连县| 宁河县| 锦州市| 承德县| 东山县| 福鼎市| 大石桥市|