posts - 7,  comments - 8,  trackbacks - 0
          近來使用hbernate越多,越覺得他的關(guān)系映射不簡單。
          如果用錯映射策略,很可能導(dǎo)致表設(shè)計的修改。
          下面我們分析一下他的多對多關(guān)系,
          其唯一的用途就是維護關(guān)系數(shù)據(jù)庫中兩張表或者多張表之間的關(guān)系
          如最典型的權(quán)限設(shè)計時,基于組-角色-用戶的設(shè)計,
          三者之間分別為多對多的關(guān)系,換言之,組可以有多個角色,角色也可以屬于多個組。

          group:
          properties:id,name,description,roleSet,userSet

          role
          properties:id,name,description,groupset

          多對多關(guān)系需要一個中間表來維護(group_role)
          group.hbm.xml
          ?1<hibernate-mapping>
          ?2????<class?name="model.GroupInfo"?table="GROUP_INFO"?lazy="false">
          ?3????????<id?name="id"?type="long"?column="ID">
          ?4????????????<generator?class="native"/>
          ?5????????</id>
          ?6????????<property?name="name"?column="name"?length="20"?type="string"/>
          ?7????????<property?name="description"?column="description"?type="string"/>
          ?8????????<set?name="userSet"?table="group_user"?cascade="save-update"?lazy="true">
          ?9????????????<key?column="group_id"/>
          10????????????<many-to-many?column="user_id"?class="com.essp.fable.model.UserInfo"/>
          11????????</set>
          12????????<set?name="roleSet"?table="group_role"?cascade="save-update"?lazy="true">
          13????????????<key?column="group_id"/>
          14????????????<many-to-many?column="role_id"?class="com.essp.fable.model.RoleInfo"/>
          15????????</set>
          16????</class>
          17</hibernate-mapping>
          role.hbm.xml
          ?1<hibernate-mapping>
          ?2????<class?name="model.RoleInfo"?table="ROLE_INFO"?lazy="false">
          ?3????????<id?name="id"?type="long"?column="ID">
          ?4????????????<generator?class="native"/>
          ?5????????</id>
          ?6????????<property?name="name"?column="name"?length="20"?type="string"/>
          ?7????????<set?name="groupSet"?table="group_role"?cascade="save-update"?inverse="true"?lazy="true">
          ?8????????????<key?column="role_id"/>
          ?9????????????<many-to-many?column="group_id"?class="com.essp.fable.model.GroupInfo"/>
          10????????</set>
          11????</class>
          12</hibernate-mapping>

          我們大都以set集合來實現(xiàn)他們的映射關(guān)系,配置xml的時候,最重的幾個屬性就是cascadeinverse
          cascade翻譯為 小瀑布, 噴流,在這里表示對象間的級聯(lián)關(guān)系,可以分別取值 為all,none,save-update,delete,all-delete-orphan,delete-orphan

          cascade屬性并不是多對多關(guān)系一定要用的,有了它只是讓我們在插入或刪除對像時更方便一些,只要在cascade的源頭上插入或是刪除,所有cascade的關(guān)系就會被自己動的插入或是刪除。
          為了能正確的cascade,unsaved-value是個很重要的屬性。Hibernate通過這個屬性來判斷一個對象應(yīng)該save還是update,如果這個對象的id是unsaved-value的話,那說明這個對象不是persistence object要save(insert);如果id是非unsaved-value的話,那說明這個對象是persistence object(數(shù)據(jù)庫中已存在),只要update就行了。saveOrUpdate方法用的也是這個機制。
          如unsaved-value="0",那當(dāng)該值為0時,hibernate即判斷其為未存儲的對像,當(dāng)執(zhí)行saveOrUpdate時,實際執(zhí)行的是save了。
          In Hibernate3, it is no longer necessary to specify an unsaved-value explicitly. 不過在hibernate3中我們不需要顯示的去指定他,hiberante會自動差別出該對像是否是新產(chǎn)生的對象。

          inverse翻譯為 倒轉(zhuǎn)的, 反轉(zhuǎn)的。他屬于關(guān)系和對象的級聯(lián)關(guān)系,默認(rèn)時為false,,就是說關(guān)系的兩端都來維護關(guān)系.如果一邊設(shè)為true,那表示他們之間的關(guān)系由對方來維護。
          比如上面的例子,我們設(shè)role的inverse為true,即他們的關(guān)系有g(shù)roup來維護。
          GroupInfo group = new GroupInfo();
          group.setName("admin");
          RoleInfo role = new RoleInfo();
          role.setName("acl");
          group.addRole(role);
          save(group);
          執(zhí)行完這些操作后,hibernate會自動保存group,role,若反之,則只會保存role.
          posted on 2006-06-08 17:24 鹽巴 閱讀(2224) 評論(1)  編輯  收藏 所屬分類: JAVA

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 金坛市| 乐亭县| 会泽县| 四平市| 武安市| 临安市| 香格里拉县| 尚志市| 新闻| 托克托县| 东山县| 永安市| 湘潭市| 成安县| 大兴区| 方山县| 唐山市| 林周县| 宜州市| 安乡县| 漳州市| 白山市| 靖远县| 石屏县| 淮滨县| 霸州市| 巧家县| 文成县| 大宁县| 普格县| 五寨县| 湖南省| 廊坊市| 庆元县| 崇州市| 观塘区| 普兰店市| 新建县| 潜江市| 英德市| 六枝特区|