为什么Hibernate的ManyToOne与mappedBy正在进行连接?

时间:2014-02-09 13:08:27

标签: java hibernate jpa many-to-one

我有以下映射实体:

@Entity
@Table(name = "survey")
public class Survey implements Serializable {
  @OneToMany(mappedBy = "survey", fetch = FetchType.EAGER)
  private List<Question> questions = new ArrayList<Question>();
}

@Entity
@Table(name = "question")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "question_type", discriminatorType = DiscriminatorType.STRING)
public abstract class Question implements Serializable {
  @ManyToOne
  @JoinColumn(name = "fk_survey", nullable = false, updatable = false, foreignKey = @ForeignKey(name = "fk_question_survey"))
  private Survey survey;
}

@Entity
@DiscriminatorValue("SINGLE_CHOICE")
public class SingleChoiceQuestion extends Question {
  @OneToMany(mappedBy = "question", fetch = EAGER)
  private List<QuestionChoice> choices = new ArrayList<QuestionChoice>();
}

@Entity
@Table(name = "question_choice")
public class QuestionChoice implements Serializable {
  @ManyToOne
  @JoinColumn(name = "fk_question", nullable = false, updatable = false, foreignKey = @ForeignKey(name = "fk_question_choice_question"))
  private Question question;
}

当我调用XXXX.getSurvey()。getQuestions()时,hibernate对此查询进行两次连接,一次检索问题,因为提取是EAGER而另一种是检索问题选择,因为获取“选择”是EAGER太。 XXXX.getSurvey()。getQuestions()应该只返回8个问题,但是因为存在36个选择,所以我会回答36个问题。

为什么hibernate有这种行为?为什么它不仅仅分开了8个问题?

0 个答案:

没有答案