天花費(fèi)了n久時(shí)間在一個(gè)
hibernate的雙鍵關(guān)聯(lián)問(wèn)題上好在最后問(wèn)題還是解決了,不然我會(huì)睡不著覺(jué)的
問(wèn)題:
我的數(shù)據(jù)庫(kù)結(jié)構(gòu)是這樣的:

首先一開始我可以獲得一個(gè)頻道的channelId,我根據(jù)這個(gè)channelId得到一個(gè)首頁(yè)區(qū)塊的List,我在hibernate中配置homepagearea的加載方式,這樣就可以通過(guò)homepage的到關(guān)聯(lián)的欄目column(多對(duì)一關(guān)系),然后我還是使用hibernate的自動(dòng)加載,取到column關(guān)聯(lián)的專題subject(一對(duì)多關(guān)系)。
這時(shí)候問(wèn)題出來(lái)了,由于column到subject的關(guān)聯(lián)沒(méi)有帶channel信息,所以,我取到的subject實(shí)際上是一個(gè)column下所有的subject,而我期望的是要得到,一個(gè)homepagearea下根據(jù)channelId和columnId取得的subject.
解決思路:
希望通過(guò)hibernate直接建立homepage和subject的1對(duì)多關(guān)聯(lián)關(guān)系
解決方法
首先我改變?cè)瓉?lái)利用工具生成的hibernate配置文件和entitybean
先列出原來(lái)的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形成一種一對(duì)多的關(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一對(duì)多映射的一端必須是主鍵,而且我這里需要關(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多對(duì)一關(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)來(lái)了
最后的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、本來(lái)使用hibernate的加載策略就是為了把一些業(yè)務(wù)邏輯直接融合在數(shù)據(jù)庫(kù)關(guān)系當(dāng)中,但是由于自己邏輯沒(méi)有考慮清楚造成了加載時(shí)信息的丟失(而且我覺(jué)得我的表結(jié)構(gòu)有問(wèn)題,不知有有沒(méi)有dba給我指點(diǎn)一下問(wèn)題)。
2、hibernate的一對(duì)多關(guān)聯(lián)關(guān)系多端是1端是針對(duì)主鍵的,所以不論你是關(guān)聯(lián)的是1列2列還是3列,它們都應(yīng)該是你的1端的主鍵或者聯(lián)合主鍵(其實(shí)一對(duì)一,多對(duì)一,多對(duì)多的原理也是相似的)。
參考資料:hibernate_reference(3.2)
posted on 2006-11-08 00:42
rocket 閱讀(2521)
評(píng)論(0) 編輯 收藏