2

我正在使用spring,带有hibernate和Mysql db。我有 Question 对象,它有一组 Answer 对象。

我的 question.hbm.xml 是

    <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.gamesapp.model.Question" table="question" >
         <cache usage="read-write"/>
        <id name="questionId" type="java.lang.Long">
            <column name="question_id" />
            <generator class="identity" />
        </id>
        <property name="description" type="java.lang.String">
            <column name="description" length="500" not-null="true" />
        </property>
        <property name="difficultyLevel" type="java.lang.String">
            <column name="difficulty_level" length="6" not-null="true" />
        </property>
        <property name="isMixer" type="java.lang.String">
            <column name="is_mixer" length="5" not-null="true" />
        </property>
        <property name="status" type="java.lang.String">
            <column name="status" length="7" not-null="true" />
        </property>
        <set name="answers"  inverse="true"   >
         <cache usage="read-write"/>
            <key>
                <column name="question_id" not-null="true" />
            </key>
            <one-to-many class="com.gamesapp.model.Answer" />
        </set>

        <set name="questionAnswerUsers"  inverse="true" >
         <cache usage="read-write"/>
            <key>
                <column name="question_id" not-null="true" />
            </key>
            <one-to-many class="com.gamesapp.model.QuestionAnswerUser" />
        </set>
    </class>
</hibernate-mapping>

我的 answer.hbm.xml 是

    <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.gamesapp.model.Answer" table="answer" >
       <cache usage="read-write"/>
        <id name="answerId" type="java.lang.Long">
            <column name="answer_id" />
            <generator class="identity" />
        </id>
        <many-to-one name="question" class="com.gamesapp.model.Question" fetch="select">
            <column name="question_id" not-null="true" />
        </many-to-one>
        <property name="description" type="java.lang.String">
            <column name="description" length="100" not-null="true" />
        </property>
        <property name="dragPosition" type="java.lang.Short">
            <column name="drag_position" />
        </property>
        <property name="orderSequence" type="java.lang.Integer">
            <column name="order_sequence" not-null="true" />
        </property>
        <property name="isAnswer" type="java.lang.String">
            <column name="is_answer" length="5" not-null="true" />
        </property>

        <property name="askPercentage" type="java.lang.Double">
            <column name="ask_percentage"  not-null="true" />
        </property>
        <set name="questionAnswerUsers"  inverse="true">
           <cache usage="read-write"/>
            <key>
                <column name="answer_id" not-null="true" />
            </key>
            <one-to-many class="com.gamesapp.model.QuestionAnswerUser" />
        </set>
    </class>
</hibernate-mapping>

现在我的问题是,我想以随机顺序获取问题列表。对于每个 Question 对象,我想使用休眠标准急切地获取答案集,并且答案集也应该随机初始化。

例如,说 Question 对象 questionOne 有一组答案,有四个 Answer object.say ans1、ans2、ans3 和 ans4。现在我第一次获取 questionOne,答案集应该初始化为 answer={ans1,ans2,ans3,ans4} 第二次获取 questionOne 对象我应该得到答案集为 answer={ans4,ans2,ans3,ans1}。

问题列表应该随机初始化。任何问题的答案集都应该使用休眠标准快速获取,每次都应该洗牌谁的项目。

提前致谢。

4

1 回答 1

3

只需使用Collections.shuffle()来获得一个随机列表:

List<Question> questions = em.createQuery(...).getResultList();
Collections.shuffle(questions);

在 Question 实体中:

public List<Answer> getShuffledAnswers() {
    List<Answer> result = new ArrayList<Answer>(this.answers);
    Collections.shuffle(result);
    return result;
}
于 2013-01-04T07:27:29.580 回答