问题:
我的数据库结构是这样的:
首先一开始我可以获得一个频道的channelId,我根据这个channelId得到一个首页区块的List,我在hibernate中配置homepagearea的加载方式,这样就可以通过homepage的到关联的栏目column(多对一关系),然后我还是使用hibernate的自动加载,取到column关联的专题subject(一对多关系)。
这时候问题出来了,由于column到subject的关联没有带channel信息,所以,我取到的subject实际上是一个column下所有的subject,而我期望的是要得到,一个homepagearea下根据channelId和columnId取得的subject.
解决思路:
希望通过hibernate直接建立homepage和subject的1对多关联关系
解决方法
首先我改变原来利用工具生成的hibernate配置文件和entitybean
先列出原来的homepagearea的配置文件
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping></hibernate-mapping>
<class></class> name="com.easou.wapsearch.channel.entity.CsHomepageArea"
table="CS_HOMEPAGE_AREA"
schema="WAPUSER"
lazy="true"
>
name="id"
type="long"
column="ID"
length="22"
>
<generator class="assigned"></generator>
name="createdBy"
type="long"
column="CREATED_BY"
length="22"
/>
name="createdDate"
type="timestamp"
column="CREATED_DATE"
length="7"
/>
name="isMore"
type="long"
column="IS_MORE"
length="22"
/>
name="name"
type="string"
column="NAME"
length="50"
/>
name="rowCount"
type="long"
column="ROW_COUNT"
length="22"
/>
name="showCount"
type="long"
column="SHOW_COUNT"
length="22"
/>
name="theOrder"
type="long"
column="THE_ORDER"
length="22"
/>
name="updateBy"
type="long"
column="UPDATE_BY"
length="22"
/>
name="updateDate"
type="timestamp"
column="UPDATE_DATE"
length="7"
/>
name="csChannel"
>
<column></column>
name="csColumn"
>
<column></column>
为了让homepagearea和subject形成一种一对多的关系,我增加的一个
lazy="false" order-by="THE_ORDER" table="CS_SUBJECT"
outer-join="true">
<key></key>
<column name="CHANNEL_ID" index="CHANNEL_ID"></column>
<column name="COLUMN_ID" index="COLUMN_ID"></column>
class="com.easou.wapsearch.channel.entity.CsSubject" />
但是由于hibernate一对多映射的一端必须是主键,而且我这里需要关联的还是2列信息,所以我还必须要修改配置文件的主键设置
<composite-id unsaved-value="none" mapped="false"></composite-id>
<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>
并且遮蔽掉原有的channel和column多对一关联关系,否则会报告重复错误
这样hibernate才会在加载homepagearea时自动把subject的关联信息也加载进来了
最后的homepagearea配置文件
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping></hibernate-mapping>
<class name="com.easou.wapsearch.channel.entity.CsHomepageArea"></class> table="CS_HOMEPAGE_AREA" schema="WAPUSER" lazy="true">
<composite-id unsaved-value="none" mapped="false"></composite-id>
<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>
<property type="long" name="createdBy" column="CREATED_BY"></property> length="22" />
<property type="timestamp" name="createdDate"></property> column="CREATED_DATE" length="7" />
<property type="long" name="isMore" column="IS_MORE"></property> length="22" />
<property></property>
<property type="long" name="rowCount" column="ROW_COUNT"></property> length="22" />
<property type="long" name="showCount" column="SHOW_COUNT"></property> length="22" />
<property type="long" name="theOrder" column="THE_ORDER"></property> length="22" />
<property type="long" name="updateBy" column="UPDATE_BY"></property> length="22" />
<property type="timestamp" name="updateDate"></property> column="UPDATE_DATE" length="7" />
lazy="false" order-by="THE_ORDER" table="CS_SUBJECT"
outer-join="true">
<key></key>
<column name="CHANNEL_ID" index="CHANNEL_ID"></column>
<column name="COLUMN_ID" index="COLUMN_ID"></column>
class="com.easou.wapsearch.channel.entity.CsSubject" />
经验总结:
1、本来使用hibernate的加载策略就是为了把一些业务逻辑直接融合在数据库关系当中,但是由于自己逻辑没有考虑清楚造成了加载时信息的丢失(而且我觉得我的表结构有问题,不知有有没有dba给我指点一下问题)。
2、hibernate的一对多关联关系多端是1端是针对主键的,所以不论你是关联的是1列2列还是3列,它们都应该是你的1端的主键或者联合主键(其实一对一,多对一,多对多的原理也是相似的)。
参考资料:hibernate_reference(3.2)