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>