嵌套的异常是org.hibernate.PropertyAccessException:无法设置字段值

时间:2019-04-12 05:21:30

标签: java database hibernate spring-boot jpa

我正在研究一个Spring-boot 2项目,该项目用于添加MCQ问题,选项和答案。一个问题的选项可能从2到6个不等。

我已经创建了3个表:一个用于“问题”,一个用于“选项”,一个用于“答案”。

下面是我在Spring Boot中的模型。

我在邮递员中收到请求时遇到OptionModel和AnswerModel错误。

请帮助解决错误并帮助我改善代码。

Question.java

@Entity
@Table(name = "mcq_question")
public class Question {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long questionId;

@Column(nullable = false)
private String question;

public Question() { }

public Long getQuestionId() {
    return questionId;
}

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

public String getQuestion() {
    return question;
}

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

@Override
public String toString() {
    return "Question{" +
            "questionId=" + questionId +
            ", question='" + question + '\'' +
            '}';
}
}

Options.java:

@Entity
@Table(name = "mcq_Options")
public class Option {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "option_id")
private int optionId;

private String option;

@ManyToOne(fetch = FetchType.LAZY, targetEntity = Question.class)
@JoinColumn(name = "questionId", nullable = false )
private Question questionId;

public Option() { }

public Option(String option, Question questionId) {
    this.option = option;
}

public int getOptionId() {
    return optionId;
}

public void setOptionId(int optionId) {
    this.optionId = optionId;
}

public String getOption() {
    return option;
}

public void setOption(String option) {
    this.option = option;
}

public Question getQuestionId() {
    return questionId;
}

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

@Override
public String toString() {
    return "Option{" +
            "optionId=" + optionId +
            ", option='" + option + '\'' +
            ", questionId=" + questionId +
            '}';
}
}

Answer.java

@Entity
@Table(name = "mcq_answer")
public class Answer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int answerId;

@OneToOne(fetch = FetchType.LAZY,  targetEntity = Question.class)
@JoinColumn(name = "questionId", nullable = false)
private int questionId;

@OneToOne(fetch = FetchType.LAZY,  targetEntity = Option.class)
@JoinColumn(name = "optionId", nullable = false)
private int optionId;

public Answer(){}

public Answer(int questionId, int optionId) {
    this.questionId = questionId;
    this.optionId = optionId;
}

public int getAnswerId() {
    return answerId;
}

public void setAnswerId(int answerId) {
    this.answerId = answerId;
}

public int getQuestionId() {
    return questionId;
}

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

public int getOptionId() {
    return optionId;
}

public void setOptionId(int optionId) {
    this.optionId = optionId;
}

@Override
public String toString() {
    return "Answer{" +
            "answerId=" + answerId +
            ", questionId=" + questionId +
            ", optionId=" + optionId +
            '}';
}
}

邮递员错误:

获取问题的请求工作正常。

获取选项表的请求:

{
"timestamp": "2019-04-12T05:18:09.831+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->com.bluepi.TrainingProject.model.Option[\"questionId\"]->com.bluepi.TrainingProject.model.Question$HibernateProxy$RND9ZBYW[\"hibernateLazyInitializer\"])",
"path": "/api/admin/viewmcqop"
}

获取答案表请求:

{
"timestamp": "2019-04-12T04:56:59.823+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Could not set field value [Option{optionId=2, option='option2', questionId=Question{questionId=1, question='Question1'}}] value by reflection : [class com.bluepi.TrainingProject.model.Answer.optionId] setter of com.bluepi.TrainingProject.model.Answer.optionId; nested exception is org.hibernate.PropertyAccessException: Could not set field value [Option{optionId=2, option='option2', questionId=Question{questionId=1, question='Question1'}}] value by reflection : [class com.bluepi.TrainingProject.model.Answer.optionId] setter of com.bluepi.TrainingProject.model.Answer.optionId",
"path": "/api/admin/viewmcqans"
}

1 个答案:

答案 0 :(得分:2)

Answer类有关的问题

在您的Answer类中,您在关系中使用了错误的字段类型。关系应该是您所涉及的类的类型,而不是id字段的类型。

所以您的Answer类应该是这样的:

@Entity
@Table(name = "mcq_answer")
public class Answer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int answerId;

@OneToOne(fetch = FetchType.LAZY,  targetEntity = Question.class)
@JoinColumn(name = "questionId", nullable = false)
private Question questionId;

@OneToOne(fetch = FetchType.LAZY,  targetEntity = Option.class)
@JoinColumn(name = "optionId", nullable = false)
private Option optionId;

public Answer(){}

public Answer(Question questionId, Option optionId) {
    this.questionId = questionId;
    this.optionId = optionId;
}

public int getAnswerId() {
    return answerId;
}

public void setAnswerId(int answerId) {
    this.answerId = answerId;
}

public Question getQuestionId() {
    return questionId;
}

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

public Option getOptionId() {
    return optionId;
}

public void setOptionId(Option optionId) {
    this.optionId = optionId;
}

@Override
public String toString() {
    return "Answer{" +
            "answerId=" + answerId +
            ", questionId=" + questionId.getQuestionId() +
            ", optionId=" + optionId.getOptionId() +
            '}';
}
}

嵌套异常

this问题中所示:您正在尝试在未加载相关对象的同时序列化子级(正在使用FetchType.LAZY)。

您有2个选择:

  • 使用FetchType.LAZY代替使用FetchType.EAGER(如果您的数据库更复杂,可能不是最佳解决方案)。

  • Option注释Question@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})类。这将忽略这些类的序列化。