列表未保存

时间:2018-06-10 18:50:03

标签: spring-data-jpa

我试图保存一个问题选项列表但是没有保存。只保存最后一行。

以下是代码。

@Transactional
public void addQuestionOptions(QuestionOptionsRequest questionOptionsRequest, int questionId) {
    List<QuestionOption> optionList = new ArrayList<>();
    QuestionOption options = new QuestionOption();

    Question question = questionRepository.findByQuestionId(questionId);
    if(question != null) {
        questionOptionsRequest.getQuestionOptions()
                .stream()
                .forEach(option -> {
                    options.setQuestionOption(option.getQuestionOption());
                    options.setQuestion(question);
                    options.setQuestionOptionNumber(option.getQuestionOptionNumber());
                    optionList.add(options);
                });
        questionOptionRepository.saveAll(optionList);
    } 
}

QuestionOption

@Entity
@JsonIgnoreProperties({"question"})
public class QuestionOption {

    @Id
    @GeneratedValue
    private int questionOptionId;

    private int questionOptionNumber;

    private String questionOption;

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
    private Question question;

    public void setQuestionOptionNumber(int questionOptionNumber)
    {
        this.questionOptionNumber = questionOptionNumber;
    }

    public void setQuestionOption(String questionOption)
    {
        this.questionOption = questionOption;
    }

    public String getQuestionOption()
    {
        return this.questionOption;
    }

    public int getQuestionOptionNumber()
    {
        return this.questionOptionNumber;
    }

    public int getQuestionOptionId() {
        return questionOptionId;
    }

    public void setQuestionOptionId(int questionOptionId) {
        this.questionOptionId = questionOptionId;
    }
}

问题

@Entity
@Getter
@Setter
public class Question {

    @Id
    @GeneratedValue
    private int questionId;

    private int assessmentId;

    private QuestionTypes questionType;

    private String questionText;

    private String questionURL;

    private QuestionStatus questionStatus;

    @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="question")
    private List<QuestionOption> questionOptions;


    public void setQuestionId(int queId)
    {
        this.questionId = queId;
    }

    public void setQuestionText(String queTxt)
    {
        this.questionText = queTxt;
    }   

    public void setQuestionType(QuestionTypes queType)
    {
        this.questionType = queType;
    }

    public void setQuestionURL(String queURL)
    {
        this.questionURL = queURL;
    }

    public int getQuestionId() {
        return questionId;
    }

    public String getQuestionText() {
        return questionText;
    }


    public QuestionStatus getQuestionStatus() {
        return questionStatus;
    }

    public void setQuestionStatus(QuestionStatus questionStatus) {
        this.questionStatus = questionStatus;
    }

    public QuestionTypes getQuestionTypes() {
        return this.questionType;
    }

    public String getQuestionURL() {
        return this.questionURL;
    }

    public int getAssessmentId() {
        return this.assessmentId;
    }

    public void setAssessmentId(int assessmentId) {
        this.assessmentId = assessmentId;
    }

    public QuestionTypes getQuestionType() {
        return questionType;
    }
}

1 个答案:

答案 0 :(得分:0)

您只创建了QuestionOption的一个对象

QuestionOption options = new QuestionOption();

在每个内部创建此对象。