jpa manytomany用于一个子类,onetomany用于第二个子类

时间:2015-12-16 10:30:20

标签: java hibernate jpa

我有以下型号: 一个类Student和超类Project,它有2个子类SmallProject和LargeProject。 只有一名学生可以在SmallProject上工作,但许多学生在LargeProject上工作。 学生可能有很多项目。

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "PROJECT")
public class Project {
   @Id
   @Column(name="id")
   @GeneratedValue(strategy = GenerationType.AUTO
   public int getId(){ return id;}

   /**
   Other attributes
   */
}


@Entity
@Table(name = "SMALL_PROJECT")
@PrimaryKeyJoinColumn(name = "ID")
public class SmallProject extends Project {

   @ManyToOne(cascade= CascadeType.ALL)
   @JoinColumn(name = "STUDENT_ID")
   private Student student;

   /**
   Other attributes
   */
}

@Entity
@Table(name = "LARGE_PROJECT")
@PrimaryKeyJoinColumn(name = "ID")
public class LargeProject extends Project {

   @ManyToMany
   @JoinTable(
        name = "STUDENT_LARGE_PROJECTS",
        joinColumns = {@JoinColumn(name="LARGE_PROJECT_ID", referencedColumnName = "ID")},
        inverseJoinColumns = {@JoinColumn(name="STUDENT_ID", referencedColumnName = "ID")}
)
   private List<Student> studentList;

   /**
   Other attributes
   */
}

我的问题是如何在Student实体中定义关系。获取所有项目的列表,而不是SmallProject列表和LargeProject列表。

@Entity
@Table(name= "STUDENT")
public class Student {

  @??
  private List<Project> projectList;

   /**
   Other attributes
   */


// This works, but i have two lists
@ManyToMany(mappedBy = "studentList")
private List<LargeProject> LargeProject;

@OneToMany(mappedBy = "student")
private List<SmallProject> smallProject;

}

如果在Project和Student之间定义manyTomany关系,它就有效。但这意味着SmallProject也可能有很多学生。

知道怎么做吗?感谢

0 个答案:

没有答案