我正在使用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对象,我都希望使用hibernate标准来获取答案集,并且答案集也应该随机初始化。
例如说问题对象questionOne有一组答案有四个Answer object.say ans1,ans2,ans3和ans4。现在我第一次获取questionOne时,应该将answer设置为answer = {ans1,ans2,ans3,ans4}。第二次我获取questionOne对象我应该得到答案设置为answer = {ans4,ans2,ans3,ans1}。
问题清单应随机初始化。 永远问题的答案集应该使用休眠标准来获取,每次都应该洗牌。
提前致谢。
答案 0 :(得分:2)
只需使用Collections.shuffle()
获取随机列表:
List<Question> questions = em.createQuery(...).getResultList();
Collections.shuffle(questions);
在问题实体中:
public List<Answer> getShuffledAnswers() {
List<Answer> result = new ArrayList<Answer>(this.answers);
Collections.shuffle(result);
return result;
}