hibernate有两个一对多的Set时怎么写hbm

2019-03-25 13:39|来源: 网路

有两个类,Course和Student,其中Student里面有两个放Course的Set:
class Course
{
...
};
class Student
{
  private Set<Course> goodCourseSet;
  private Set<Course> badCourseSet;
...
};

问题描述:
如果Student里只有一个Course的Set,底层Course表只要加一列StudentID属性就可以了,但现在Student对应了两个Course集,仅仅加一列StudentID属性肯定已经区分不出来到底对应哪个Course集了,所以底层表结构要变,这样hbm文件肯定也要变,但我不知该怎么变。

目前已有的分析:
下面是我猜想出来的一种写法,由于需要区分出某Student的Course是good还是bad,需要在Course表中增加两个字段,studentID和isGood,isGood的值就是用来区分不同CourseSet的,我下面写的只是表示个大概意思,其中 value只是希望用来区分是哪个Set,也许实际当中不存在这个属性。
<set name= "goodCourseSet" > 
  <key > 
  <column name= "student_id" /> 
  <column name= "isGood" value="1" /> 
  </key> 
  <one-to-many class= "Course"/> 
</set> 
<set name= "goodCourseSet" > 
  <key > 
  <column name= "studentID" /> 
  <column name= "isGood" value="0" /> 
  </key> 
  <one-to-many class= "Course"/> 
</set>

相关问答

更多