尝试在休眠模式下保存数据时出错

时间:2018-07-30 18:00:11

标签: java hibernate

我在尝试添加问题(addQuestion)

时遇到异常
attempted to assign id from null one-to-one property 
[com.lte.assessment.model.Question.assessment];

嵌套的例外是:

org.hibernate.id.IdentifierGenerationException: attempted to assign id from
null one-to-one property [com.lte.assessment.model.Question.assessment]

我不想在添加问题时保存评估,因为它是单独完成的,那么我应该如何正确解决呢? < / p>

我尝试jsonignoreproperties,但是它不起作用,我目前的解决方法是将@MapsId更改为@JoinColumn("assessmentId", insertable=false,updatable=false)

Assessment

@Entity
@JsonIgnoreProperties({"questionList"})
public class Assessment {

    @Id
    private String assessmentId;

    private String assessmentTopic;

    private String assessmentSubTopic;

    private String assessmentLevel;

    private String createdBy;

    private String rating;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assessment", fetch = FetchType.LAZY)
    private List<Question> questionList;
//....
}


Question


@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
    @MapsId("assessmentId")
    private Assessment assessment;

    public Assessment getAssessment() {
        return assessment;
    }

    public void setAssessment(Assessment assessment) {
        this.assessment = assessment;
    }
    //    private int questionNumber;

    private QuestionTypes questionType;

    private String questionText;

    private String questionURL;

    private QuestionStatus questionStatus;
//...
}


@Embeddable
public class QuestionAssessmentKey implements Serializable {

    private int questionNumber;

    private String assessmentId;

    public QuestionAssessmentKey(int questionNumber) {
        this.questionNumber = questionNumber;
    }

    public int getQuestionNumber() {
        return questionNumber;
    }

    public void setQuestionNumber(int questionNumber) {
        this.questionNumber = questionNumber;
    }

    public String getAssessmentId() {
        return assessmentId;
    }

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

0 个答案:

没有答案