读书人

Hibernate的List投射

发布时间: 2012-10-29 10:03:53 作者: rapoo

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

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

当使用list标签映射时,Question.hbm.xml中的配置如下:

<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 name="code"><bag name="answers" order-by="id asc" lazy="false">    <key column="question_id" />    <one-to-many class="mypackage.Answer"/></bag>
1 楼 qinglintan 2010-10-21 这位兄弟似乎做过问卷生成

读书人网 >软件架构设计

热点推荐