有ExamPaper考卷类,对应EXAMPAPER表,有栏位 id name
Question题目类(放在题库中的题),对应QUESTION表,有栏位 id content type
ExamQuestion考题类(考卷上的题目),对应EXAMPAPER_QUESTION表,有栏位 id exampaper_id question_id score
题目分选择 填空 解答3个类型,我分别映射了ChoiceQuestion FillingQuestion ProcessQuestion 3个Question的子类,但我希望在ExamPaper类中取得此考卷所有的选择题或所有的填空题,该如何配置?或者是我的设计有问题,怎么修正?
[b]问题补充:[/b]
谢谢wangxin0072000的回答,但你没明白我的意思,ExamQuestion是ExamPaper和Question的中间类,体现了考卷和题目的关系,我想知道怎样使ExamQuestion能够表现出Question的分类,即Question的子类ChoiceQuestion FillingQuestion ProcessQuestion分别对应ExamQuestion的子类ChoiceExamQuestion FillingExamQuestion ProcessExamQuestion,因为我想在ExamPaper类中加入方法getChoiceQuestions(),返回的是考卷中所有选择题。
[b]问题补充:[/b]
嗯?意思是ExamPaper和Question的子类多对多关联,中间表为EXAMPAPER_QUESTION,因为我需要中间表的score 属性,所以再建一个ExamQuestion类与中间表对应,要从ExamPaper取出每题的得分,那么ExamPaper又要与ExamQuestion-对多关联,这样ExamPaper<-1---n->ExamQuestion<-m---1->Question,ExamPaper<-n---m->Question.是这样吗?这样配置不会有脏数据问题吗?
[b]问题补充:[/b]
可能是设计的问题吧,你能帮我想个解决办法么?需求如下:
随机从题库产生考卷,考卷明细包括题型 总分 总得分 每题分数 每题得分.
[b]问题补充:[/b]
对于maxwell的建议,那么如何配置以保证ExamQuestion和Question的type一一对应呢?
难道要每次都examQuestion.setType(“A”)?
首先,我认为没有必要建立ChoiceExamQuestion FillingExamQuestion ProcessExamQuestion这3类,因为这三个类的信息都是完全一样的。如果作为关系表,那么一个examquestion足够了。我的第二个xml示例
[code="xml"]
.......
<!--下面这几行指明了ExamPaper类中的choiceQuestion属性关联的是ChoiceQuestion集合,而关联关系来自于examquestion表 -->
......
......
[/code]
其次,在EamPaper类中有choiceQuestion属性。直接getChoiceQuestion就可以得到ChoiceQuestion的Set。
最后,如果你想得到ExamQuestion这个表中的数据那么建立一个实体bean和对应关系,就可以了,没有必要建3个。
如果你已经建立好ChoiceQuestion FillingQuestion ProcessQuestion3个实体类。那么使用继承关系,使这三个类继承子Question。之后使用Hibernate的继承映射。这种映射可以保证多个实体类出自一个数据库表。配置如下。具体可以参考Hibernate reference。
最后Exampaper与Question建立多对多关系。我写出了大概的配置和代码,有可能有出入。你在改改。
[code="xml"]
......
......
......
......
[/code]
[code="java"]
class ExamPaper {
....
Set choiceQuestion=new HashSet();
Set fillingQuestion=new HashSet();
Set processQuestion=new HashSet();
}
[/code]
[code="xml"]
.......
......
......
[/code]
基本就是这个意思。难点在于Question子类的配置,看我的第一个XML代码。
[quote]
要从ExamPaper取出每题的得分,那么ExamPaper又要与ExamQuestion-对多关联
[/quote]
这样,好像是比较麻烦,但是也没办法,因为一般中间关系表中只存两个表的主键,而你的score不应该存在这里。应该存在莫个bean中,但是这个你的业务逻辑设计方面的问题了,我就不了解了。
至于脏数据的问题,应该也不会有。但是hibernate的具体设置要看你的逻辑了。如果有问题最好看看hibernate reference。其中有详细的解答。
每道考题的分数对与不同的考卷相同吗?如果相同就把score放到question里面。如果不同就单独建一个bean存放score(可以就关联ExamQuestion表)。其他的就没什么了。
在ExamQuestion中加一个column type 对于question表的 type
仅仅是个人意见, 仅供参考。