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

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

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

    Asktalk

    天行健,君子以自強不息!
    posts - 21, comments - 79, trackbacks - 0, articles - 2
      BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

    Hibernate3之One-to-one關系映射

    Posted on 2005-08-18 00:26 Asktalk 閱讀(5253) 評論(0)  編輯  收藏 所屬分類: Jdbc/Hibernate3/EJB3

    One-to-one關系映射

     

        對于hibernateone-to-one關系來說,大家常常把它忽略,認為它很簡單,其實這里面有些細節需要注意,在hibernate3中有兩種實現one-to-one的方法:第一種就是用many-to-one來代替一對多,其實one-to-one也就是many-to-one的一種極限方式,若把many-to-one設置unique="true",則這時候的many-to-one實質上就是one-to-one;這里為什么能夠用many-to-one來代替one-to-one呢?最根本的是兩個對象必須有一個字段相關聯,那么你也發現one-to-one中沒有column屬性,也就是不能夠把one-to-one的這種關系生成一個字段/屬性,而many-to-one可以,它有column屬性。所以,說了這么多,你應該明白為什么可以用many-to-one來代替one-to-one.

     

    對于這種方法要注意幾點:

    例如,下面的介紹中use中的兩個字段同時影射adress,只能夠實現單向one-to-one,也就是從useadress,不能夠實現從adressuse.然但下面也介紹了可以使用復雜的方法來解決,但是得不償失。

     

    第二種方法:基于主鍵關聯的one-to-one,這種方法比較直接。也就是讓adress的主鍵值和use的一樣就可以了,通過

    <id name="id" column="ADDRESS_ID">

    <generator class="foreign">

    <param name="property">user</param>

    </generator>

    來把adress的主鍵值和use的主鍵值相等。其余的設置都是小事。

     

    對于初學者來說應該注意:單在useadress中設置one-to-one是不行的,需要設置上面的代碼,需要實現one-to-one兩端的對象主鍵值相同,這是最主要的。這也是one-to-onemany-to-one的不同之處。應當注意。

                                                                                               fasttalk

                                                                                  www.tkk7.com/asktalk

     

     

     

    Using a foreign key association

    The easiest way to represent the association from User to its billingAddress is to use a <many-to-one> mapping with a unique constraint on the foreign key. This may surprise you, since many doesn’t seem to be a good description of either end of a one-to-one association! However, from Hibernate’s point of view, there isn’t much difference between the two kinds of foreign key associations. So, we add a foreign key column named BILLING_ADDRESS_ID to the USER table and map it as follows:

     

    <many-to-one name="billingAddress"

    class="Address"

    column="BILLING_ADDRESS_ID"

    cascade="save-update"/>

     

    Note that we’ve chosen save-update as the cascade style. This means the Address will become persistent when we create an association from a persistent User. Probably,  cascade="all" makes sense for this association, since deletion of the User should result in deletion of the Address. (Remember that Address now has its own entity lifecycle.)

     

    Our database schema still allows duplicate values in the BILLING_ADDRESS_ID column of the USER table, so two users could have a reference to the same address. To make this association truly one-to-one, we add unique="true" to the <many-toone> element, constraining the relational model so that there can be only one user per address:

     

    <many-to-one name="billingAddress"

    class="Address"

    column="BILLING_ADDRESS_ID"

    cascade="all"

    unique="true"/>

     

    This change adds a unique constraint to the BILLING_ADDRESS_ID column in the DDL generated by Hibernate—resulting in the table structure illustrated by figure 6.7.

     

    But what if we want this association to be navigable from Address to User in Java?  From chapter 3, you know how to turn it into a bidirectional one-to-many collection—but we’ve decided that each Address has just one User, so this can’t be the right solution. We don’t want a collection of users in the Address class. Instead, we add a property named user (of type User) to the Address class, and map it like so in the mapping of Address:

     

    <one-to-one name="user"

    class="User"

    property-ref="billingAddress"/>

     

    This mapping tells Hibernate that the user association in Address is the reverse direction of the billingAddress association in User.

     

    In code, we create the association between the two objects as follows:

    Address address = new Address();

    address.setStreet("646 Toorak Rd");

    address.setCity("Toorak");

    address.setZipcode("3000");

    Transaction tx = session.beginTransaction();

    User user = (User) session.get(User.class, userId);

    address.setUser(user);

    user.setBillingAddress(address);

    tx.commit();

    image002.gif

    To finish the mapping, we have to map the homeAddress property of User. This is easy enough: we add another <many-to-one> element to the User metadata, mapping a new foreign key column, HOME_ADDRESS_ID:

     

    <many-to-one name="homeAddress"

    class="Address"

    column="HOME_ADDRESS_ID"

    cascade="save-update"

    unique="true"/>

     

    The USER table now defines two foreign keys referencing the primary key of the ADDRESS table: HOME_ADDRESS_ID and BILLING_ADDRESS_ID.

     

    Unfortunately, we can’t make both the billingAddress and homeAddress associations bidirectional, since we don’t know if a particular address is a billing address or a home address. (We can’t decide which property name—billingAddress or homeAddress—to use for the property-ref attribute in the mapping of the user property.) We could try making Address an abstract class with subclasses HomeAddress and BillingAddress and mapping the associations to the subclasses. This approach would work, but it’s complex and probably not sensible in this case.  Our advice is to avoid defining more than one one-to-one association between any two classes. If you must, leave the associations unidirectional. If you don’t have more than one—if there really is exactly one instance of Address per User—there is an alternative approach to the one we’ve just shown. Instead of defining a foreign key column in the USER table, you can use a primary key association.

     

    Using a primary key association

    Two tables related by a primary key association share the same primary key values.  The primary key of one table is also a foreign key of the other. The main difficulty with this approach is ensuring that associated instances are assigned the same primary key value when the objects are saved. Before we try to solve this problem, let’s see how we would map the primary key association.

     

    For a primary key association, both ends of the association are mapped using the <one-to-one> declaration. This also means that we can no longer map both the billing and home address, only one property. Each row in the USER table has a corresponding row in the ADDRESS table. Two addresses would require an additional table, and this mapping style therefore wouldn’t be adequate. Let’s call this single address property address and map it with the User:

     

    <one-to-one name="address"

    class="Address"

    cascade="save-update"/>

     

    Next, here’s the user of Address:

    <one-to-one name="user"

    class="User"

    constrained="true"/>

     

    The most interesting thing here is the use of constrained="true". It tells Hibernate that there is a foreign key constraint on the primary key of ADDRESS that refers to the primary key of USER.

     

    Now we must ensure that newly saved instances of Address are assigned the same identifier value as their User. We use a special Hibernate identifier-generation strategy called foreign:

     

    <class name="Address" table="ADDRESS">

    <id name="id" column="ADDRESS_ID">

    <generator class="foreign">

    <param name="property">user</param>

    </generator>

    </id>

    ...

    <one-to-one name="user"

    class="User"

    constrained="true"/>

    </class>

     

    The <param> named property of the foreign generator allows us to name a one-toone association of the Address class—in this case, the user association. The foreign generator inspects the associated object (the User) and uses its identifier as the identifier of the new Address. Look at the table structure in figure 6.8.  The code to create the object association is unchanged for a primary key association;  it’s the same code we used earlier for the many-to-one mapping style.

    image004.gif

     

    主站蜘蛛池模板: 国产精品亚洲产品一区二区三区 | 国产精品无码免费专区午夜 | 国产香蕉免费精品视频| 国产精品亚洲一区二区三区在线| h片在线观看免费| 亚洲综合色区在线观看| 亚洲一区二区三区免费| 亚洲伊人久久大香线蕉综合图片| 国产精品内射视频免费| 亚洲国产精华液网站w| 久久精品私人影院免费看| 777亚洲精品乱码久久久久久 | 亚洲精品无码aⅴ中文字幕蜜桃| 啦啦啦中文在线观看电视剧免费版| 亚洲精品亚洲人成在线播放| 好吊妞视频免费视频| 风间由美在线亚洲一区| 免费a级毛片无码av| 国产精品免费观看视频| 亚洲综合精品香蕉久久网97| 999国内精品永久免费观看| 亚洲夂夂婷婷色拍WW47| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 精品国产亚洲一区二区三区| 久久久久成人片免费观看蜜芽 | 亚洲 日韩 色 图网站| 四只虎免费永久观看| 成人免费ā片在线观看| 亚洲精品一区二区三区四区乱码| 一个人看的www在线观看免费| 国产精品亚洲天堂| 亚洲日韩中文无码久久| 色se01短视频永久免费| 粉色视频免费入口| 亚洲AV无码成人精品区蜜桃| 18禁无遮挡无码网站免费| 人妻巨大乳hd免费看| 亚洲精品电影天堂网| 免费在线不卡视频| 18女人水真多免费高清毛片 | a级毛片在线免费看|