首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Hibernate的List照射

2012-06-27 
Hibernate的List映射Answer类为Question类一对多关联关系,即一个问题对应多个答案。他们的表结构如下如果希

Hibernate的List映射
Answer类为Question类一对多关联关系,即一个问题对应多个答案。他们的表结构如下

如果希望Answer集合在Question类中作为List存储,我们可以使用hibernate的list或者bag标签来进行映射。

当使用list标签映射时,Question.hbm.xml中的配置如下:
Java代码 
<hibernate-mapping> 
    <class name="mypackage.Question" table="question"> 
        <id name="id" type="integer"> 
            <column name="id" /> 
            <generator /> 
        </id> 
        <property name="userId" type="integer"> 
            <column name="user_id" /> 
        </property> 
        <property name="content" type="string"> 
            <column name="content" length="200" /> 
        </property> 
        <property name="time" type="timestamp"> 
            <column name="time" length="19" /> 
        </property> 
        <list name="answers" inverse="true" cascade="all" lazy="false"> 
            <key column="question_id" not-null="true"/> 
            <index column="position" /> 
            <one-to-many order-by="id asc" lazy="false"> 
    <key column="question_id" /> 
    <one-to-many class="mypackage.Answer"/> 
</bag> 

热点排行