Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在jpa中查询父表时如何从子表中获取字段_Java - Fatal编程技术网

Java 在jpa中查询父表时如何从子表中获取字段

Java 在jpa中查询父表时如何从子表中获取字段,java,Java,我有两个问题表和问题选项。问题有一个复合键。当我通过id查询问题时,如何获得问题选项。如何确保我也能获得问题选项。到现在为止,我只收到了问题。我应该更改映射还是添加一些属性 问题: @Entity @Getter @Setter @JsonIgnoreProperties({"assessment"}) public class Question implements Serializable { @EmbeddedId private QuestionAssessmentKe

我有两个问题表和问题选项。问题有一个复合键。当我通过id查询问题时,如何获得问题选项。如何确保我也能获得问题选项。到现在为止,我只收到了问题。我应该更改映射还是添加一些属性

问题:

@Entity
@Getter
@Setter
@JsonIgnoreProperties({"assessment"})
public class Question implements Serializable {

    @EmbeddedId
    private QuestionAssessmentKey questionAssessmentKey;

    public QuestionAssessmentKey getQuestionAssessmentKey() {
        return questionAssessmentKey;
    }

    public void setQuestionAssessmentKey(QuestionAssessmentKey questionAssessmentKey) {
        this.questionAssessmentKey = questionAssessmentKey;
    }

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name =  "assessmentId", insertable = false, updatable = false)
    private Assessment assessment;

    private String questionText;

    private String questionURL;

    private QuestionStatus questionStatus;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumns({
            @JoinColumn(name="assessmentId", referencedColumnName = "assessmentId"),
            @JoinColumn(name="questionNumber", referencedColumnName = "questionNumber")
    })
    private List<QuestionOption> questionOptions;

    public List<QuestionOption> getQuestionOptions() {
        return questionOptions;
    }

    public void setQuestionOptions(List<QuestionOption> questionOptions) {
        this.questionOptions = questionOptions;
    }

    public Assessment getAssessment() {
        return assessment;
    }

    public void setAssessment(Assessment assessment) {
        this.assessment = assessment;
    }
    //    private int questionNumber;
    private QuestionTypes questionType;
//Getters and setters
}
问题评估键

@Embeddable
public class QuestionAssessmentKey implements Serializable {

    private int questionNumber;

    private String assessmentId;
}

当然,您不能在一个查询中获取所有数据,因为问题和问题选项之间是一对多关系。但是,当您在获取的
Question
实体上调用
getQuestionOptions
时,它应该加载并返回相应的选项集。

如果只有@ManyToOne映射,那么您可以使用它来完成此fetchType。在@ManyToOne in QuestionOption上急切吗?是的。但这并不能解决您的问题,因为当您通过ID解析QuestionOption实体时,它只会让Hibernate急切地加载相应的问题实体;但是,由于每个问题都可以有多个相应的选项,因此不能采用相反的方法。
@Embeddable
public class QuestionAssessmentKey implements Serializable {

    private int questionNumber;

    private String assessmentId;
}