继承+ JPA + @ OneToMany关系

时间:2018-03-28 09:37:02

标签: java jpa

我有2个实体候选人和候选人,每个人有2个孩子候选人X,候选人Y和候选人X,候选人资格。 候选人和候选人之间的关系是一对多

@Entity
@Table(name = "CANDIDATURE")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE )
@DiscriminatorColumn( name="CANDIDATURE_TYPE",discriminatorType=DiscriminatorType.STRING)
public  class  Candidature {

     private Candidate candidate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CANDIDATE_ID", nullable = false)
    public Candidate getCandidate() {
        return candidate;
    }}




@Entity
@Table(name = "CANDIDATE")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE )
@DiscriminatorColumn( name="CANDIDATE_TYPE",discriminatorType=DiscriminatorType.STRING)
public  class  Candidate {


 private Set<Candidature> candidatures;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "candidate")
    public Set<Candidature> getCandidatures() {
        return candidatures;
    }


  }

有了这个实现,我无法将孩子候选人X加入候选人X,我该如何解决?我可以加入儿童和如何? 这是孩子

@Entity
@DiscriminatorValue("CANDIDATURE_X")
public  class CandidatureX extends Candidature{
    }
@Entity
@DiscriminatorValue("CANDIDATURE_Y")
public  class CandidatureY extends Candidature{
    }

@Entity
@DiscriminatorValue("CANDIDATE_X")
public  class CandidateX extends Candidate{
    }

@Entity
@DiscriminatorValue("CANDIDATE_Y")
public  class CandidateY extends Candidate{
    }

我的问题在于候选X-候选人X之间的关系 和候选人Y - 候选人资格

0 个答案:

没有答案