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

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

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

    javaGrowing

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      92 隨筆 :: 33 文章 :: 49 評(píng)論 :: 0 Trackbacks
    我的數(shù)據(jù)庫結(jié)構(gòu)是這樣的:

    首先一開始我可以獲得一個(gè)頻道的channelId,我根據(jù)這個(gè)channelId得到一個(gè)首頁區(qū)塊的List,我在hibernate中配置homepagearea的加載方式,這樣就可以通過homepage的到關(guān)聯(lián)的欄目column(多對一關(guān)系),然后我還是使用hibernate的自動(dòng)加載,取到column關(guān)聯(lián)的專題subject(一對多關(guān)系)。
    這時(shí)候問題出來了,由于column到subject的關(guān)聯(lián)沒有帶channel信息,所以,我取到的subject實(shí)際上是一個(gè)column下所有的subject,而我期望的是要得到,一個(gè)homepagearea下根據(jù)channelId和columnId取得的subject.

    解決思路:
    希望通過hibernate直接建立homepage和subject的1對多關(guān)聯(lián)關(guān)系

    解決方法
    首先我改變原來利用工具生成的hibernate配置文件和entitybean
    先列出原來的homepagearea的配置文件
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    ??? "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    ??? "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

    <hibernate-mapping>
    <class
    ??? name="com.easou.wapsearch.channel.entity.CsHomepageArea"
    ??? table="CS_HOMEPAGE_AREA"
    ??? schema="WAPUSER"
    ??? lazy="true"
    >
    ??? <id
    ??????? name="id"
    ??????? type="long"
    ??????? column="ID"
    ??????? length="22"
    ??? >
    ??? ?<generator class="assigned">
    ??? ??
    ??? ?</generator>
    ??? </id>
    ??? <property
    ??????? name="createdBy"
    ??????? type="long"
    ??????? column="CREATED_BY"
    ??????? length="22"
    ??? />
    ??? <property
    ??????? name="createdDate"
    ??????? type="timestamp"
    ??????? column="CREATED_DATE"
    ??????? length="7"
    ??? />
    ??? <property
    ??????? name="isMore"
    ??????? type="long"
    ??????? column="IS_MORE"
    ??????? length="22"
    ??? />
    ??? <property
    ??????? name="name"
    ??????? type="string"
    ??????? column="NAME"
    ??????? length="50"
    ??? />
    ??? <property
    ??????? name="rowCount"
    ??????? type="long"
    ??????? column="ROW_COUNT"
    ??????? length="22"
    ??? />
    ??? <property
    ??????? name="showCount"
    ??????? type="long"
    ??????? column="SHOW_COUNT"
    ??????? length="22"
    ??? />
    ??? <property
    ??????? name="theOrder"
    ??????? type="long"
    ??????? column="THE_ORDER"
    ??????? length="22"
    ??? />
    ??? <property
    ??????? name="updateBy"
    ??????? type="long"
    ??????? column="UPDATE_BY"
    ??????? length="22"
    ??? />
    ??? <property
    ??????? name="updateDate"
    ??????? type="timestamp"
    ??????? column="UPDATE_DATE"
    ??????? length="7"
    ??? />

    ??? <!-- Associations -->
    ??? <!-- bi-directional many-to-one association to CsChannel -->
    ??? <many-to-one
    ??????? name="csChannel"???????
    ??? >
    ??????? <column name="CHANNEL_ID" length="22"/>
    ??? </many-to-one>
    ??? <!-- bi-directional many-to-one association to CsColumn -->
    ??? <many-to-one
    ??????? name="csColumn"???????
    ??? >
    ??????? <column name="COLUMN_ID" length="22"/>
    ??? </many-to-one>

    </class>
    </hibernate-mapping>

    為了讓homepagearea和subject形成一種一對多的關(guān)系,我增加的一個(gè)
    <set name="csSubjects" inverse="true" cascade="save-update"
    ???lazy="false" order-by="THE_ORDER" table="CS_SUBJECT"
    ???outer-join="true">
    ???<key>
    ????<column name="CHANNEL_ID" index="CHANNEL_ID" />
    ????<column name="COLUMN_ID" index="COLUMN_ID" />
    ???</key>
    ???<one-to-many
    ????class="com.easou.wapsearch.channel.entity.CsSubject" />
    </set>
    但是由于hibernate一對多映射的一端必須是主鍵,而且我這里需要關(guān)聯(lián)的還是2列信息,所以我還必須要修改配置文件的主鍵設(shè)置
    <composite-id mapped="false" unsaved-value="none">
    ????? <key-many-to-one name="csChannel" column="CHANNEL_ID"></key-many-to-one>
    ???<key-many-to-one name="csColumn" column="COLUMN_ID"></key-many-to-one>
    </composite-id>
    并且遮蔽掉原有的channel和column多對一關(guān)聯(lián)關(guān)系,否則會(huì)報(bào)告重復(fù)錯(cuò)誤
    <!-- Associations
    ?? bi-directional many-to-one association to CsChannel
    ??<many-to-one name="csChannel" lazy="true">
    ???<column name="CHANNEL_ID" length="22" />
    ??</many-to-one>
    ?? bi-directional many-to-one association to CsColumn
    ??<many-to-one name="csColumn" lazy="true">
    ???<column name="COLUMN_ID" length="22" />
    ??</many-to-one>

    ??-->
    這樣hibernate才會(huì)在加載homepagearea時(shí)自動(dòng)把subject的關(guān)聯(lián)信息也加載進(jìn)來了


    最后的homepagearea配置文件
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    ??? "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    ??? "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

    <hibernate-mapping>
    ?<class name="com.easou.wapsearch.channel.entity.CsHomepageArea"
    ??table="CS_HOMEPAGE_AREA" schema="WAPUSER" lazy="true">
    ??<composite-id mapped="false" unsaved-value="none">
    ????? <key-many-to-one name="csChannel" column="CHANNEL_ID"></key-many-to-one>
    ???<key-many-to-one name="csColumn" column="COLUMN_ID"></key-many-to-one>
    ??</composite-id>
    ??<property name="createdBy" type="long" column="CREATED_BY"
    ???length="22" />
    ??<property name="createdDate" type="timestamp"
    ???column="CREATED_DATE" length="7" />
    ??<property name="isMore" type="long" column="IS_MORE"
    ???length="22" />
    ??<property name="name" type="string" column="NAME" length="50" />
    ??<property name="rowCount" type="long" column="ROW_COUNT"
    ???length="22" />
    ??<property name="showCount" type="long" column="SHOW_COUNT"
    ???length="22" />
    ??<property name="theOrder" type="long" column="THE_ORDER"
    ???length="22" />
    ??<property name="updateBy" type="long" column="UPDATE_BY"
    ???length="22" />
    ??<property name="updateDate" type="timestamp"
    ???column="UPDATE_DATE" length="7" />

    ??<!-- Associations
    ?? bi-directional many-to-one association to CsChannel
    ??<many-to-one name="csChannel" lazy="true">
    ???<column name="CHANNEL_ID" length="22" />
    ??</many-to-one>
    ?? bi-directional many-to-one association to CsColumn
    ??<many-to-one name="csColumn" lazy="true">
    ???<column name="COLUMN_ID" length="22" />
    ??</many-to-one>

    ??-->
    ??<set name="csSubjects" inverse="true" cascade="save-update"
    ???lazy="false" order-by="THE_ORDER" table="CS_SUBJECT"
    ???outer-join="true">
    ???<key>
    ????<column name="CHANNEL_ID" index="CHANNEL_ID" />
    ????<column name="COLUMN_ID" index="COLUMN_ID" />
    ???</key>
    ???<one-to-many
    ????class="com.easou.wapsearch.channel.entity.CsSubject" />

    ??</set>


    ?</class>

    </hibernate-mapping>

    經(jīng)驗(yàn)總結(jié):
    1、本來使用hibernate的加載策略就是為了把一些業(yè)務(wù)邏輯直接融合在數(shù)據(jù)庫關(guān)系當(dāng)中,但是由于自己邏輯沒有考慮清楚造成了加載時(shí)信息的丟失(而且我覺得我的表結(jié)構(gòu)有問題,不知有有沒有dba給我指點(diǎn)一下問題)。
    2、hibernate的一對多關(guān)聯(lián)關(guān)系多端是1端是針對主鍵的,所以不論你是關(guān)聯(lián)的是1列2列還是3列,它們都應(yīng)該是你的1端的主鍵或者聯(lián)合主鍵(其實(shí)一對一,多對一,多對多的原理也是相似的)。

    參考資料:hibernate_reference(3.2)

    posted on 2006-11-08 11:50 javaGrowing 閱讀(379) 評(píng)論(0)  編輯  收藏

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 91麻豆国产自产在线观看亚洲| 我要看WWW免费看插插视频| 啦啦啦高清视频在线观看免费 | 免费国产成人午夜电影| 亚洲色欲色欲www在线播放| 巨波霸乳在线永久免费视频| 亚洲天天做日日做天天欢毛片 | 国产午夜鲁丝片AV无码免费 | 99久久国产精品免费一区二区 | 亚洲综合中文字幕无线码| 99视频全部免费精品全部四虎| 1区1区3区4区产品亚洲| 久久一本岛在免费线观看2020| 亚洲AV综合色区无码另类小说| 日本中文字幕免费高清视频| 亚洲国产精品线在线观看| 国内免费高清在线观看| 好男人资源在线WWW免费| 亚洲成av人片天堂网| 久久久久高潮毛片免费全部播放 | caoporm超免费公开视频| 亚洲无av在线中文字幕| 午夜理伦剧场免费| 亚洲av无码专区青青草原| 亚洲M码 欧洲S码SSS222| 四虎国产精品免费永久在线| 亚洲短视频男人的影院| 1000部拍拍拍18勿入免费视频软件| 亚洲国产人成在线观看| 午夜爱爱免费视频| 免费国产高清毛不卡片基地| 国内精品99亚洲免费高清| 最近中文字幕mv手机免费高清| 免费在线黄色电影| 色爽黄1000部免费软件下载| 国产AV无码专区亚洲精品| 国产成人免费全部网站| A片在线免费观看| eeuss影院ss奇兵免费com| 在线亚洲v日韩v| 久久亚洲国产中v天仙www |