提问者:小点点

java.sql.SqlinTegrityConstraintViolationException:列“question_id”不能为空


我试图使用spring将问题数据与答案一起保存在Mysql中。 在我的例子中,问题是一个父项,答案是一个子项,答案包含选项,包括正确的选项。 我在类里用过一对多映射。 当我持久化这些数据时,我注意到问题(父)被持久化而没有任何问题,但没有答案实体。 我试了很多方法,但都没有运气。 我很感激你的帮助。

这是我的孩子班

@Entity
@Table(name="answer")
public class Answer extends AuditModel {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

@NotNull
@NotBlank
private String qoption;

private String isCorrect;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id", referencedColumnName = "id")
private Question question; 
// and setters and getters for these 

下面是我的父类

 @Entity
 @Table(name="question")
 public class Question extends AuditModel{

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

@NotNull
@NotBlank
private String description;

@NotBlank
private String qcategory;

@NotNull
@NotBlank
private String qtype;

private String lsaQuestion;

private String clue;


@OneToMany(cascade = CascadeType.PERSIST,
        fetch = FetchType.LAZY,
        mappedBy = "question")
private List<Answer> answers;
// and setters and getters for these 

和下面的数据映射代码

     private QuestionDto createDTOObject(QuestionRequest request, String questionImageUrl, String 
     clueImageUrl, String addinfoimageUrl) {

    QuestionDto question=new QuestionDto();
    question.setDescription(request.getDescription());
    question.setClueImageUrl(clueImageUrl);
    question.setAddinfoimageUrl(addinfoimageUrl);
    question.setExplImageUrl(explImageUrl);
    List<AnswerDto> answers = mapAnswers(request,question);
    question.setAnswers(answers);

    return question;
}

/**
 * @param request
 * @return
 */
private List<AnswerDto> mapAnswers(QuestionRequest request,QuestionDto question) {
    List<AnswerDto> answers = new ArrayList<AnswerDto>();
    for (AnswerDto ad : request.getAnswers()) {
        AnswerDto dto = new AnswerDto();
        dto.setQoption(ad.getQoption());
        dto.setIsCorrect(ad.getIsCorrect());
        answers.add(dto);
    }
    return answers;
}

下面的代码用于使用spring data jpa在Impl类中持久化这两个对象

 @Override
public String create(QuestionDto dto) {
    Question model = mapper.toModel(dto);   
    quizRepository.save(model);
    return model.getId().toString();
}

我有一个答案如下

public class AnswerDto {

private String qoption;

private String isCorrect;

private QuestionDto question; 

public QuestionDto getQuestion() {
    return question;
}
public void setQuestion(QuestionDto question) {
    this.question = question;
}

我有以下问题。

public class QuestionDto {

private List<AnswerDto> answers;

    public List<AnswerDto> getAnswers() {
    return answers;
}

public void setAnswers(List<AnswerDto> answers) {
    this.answers = answers;
}

我有问题(模型)如下所示,正如我在之前发布的

@OneToMany(cascade = CascadeType.PERSIST,
        fetch = FetchType.LAZY,
        mappedBy = "question")
private List<Answer> answers = new ArrayList<>();

我有以下答案(模型)。

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id", referencedColumnName = "id")
private Question question; 

public Question getQuestion() {
    return question;
}

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

因此,我将json请求转换为QuestionDTO,并将Answer DTO添加到QuestionDTO,作为您提到的所需的双向映射。 在持久化时,我们使用下面的代码将问题DTO转换为问题(模型)。

@Autowired
private QuizMapper mapper;

@Override
public String create(QuestionDto dto) {
    Question model = mapper.toModel(dto);   
    quizRepository.save(model);
    return model.getId().toString();
}

在将DTO转换为模型时,我遇到了堆栈溢出错误。


共1个答案

匿名用户

由于定义了双向关系,因此必须在answerquestion实体中设置该关系:

    question.setAnswers(answers);
    for (var a : answers) {
        a.setQuestion(question);
    }

编辑:

因此,我将json请求转换为Question DTO,并将Answer DTO添加到Question DTO,作为您提到的所需的双向映射

我觉得你把事情搞混了。 您正在模型(实体)中使用双向关联,这意味着您的子实体存储对其父实体的引用,而父实体存储对其所有子实体的引用。 显然,在DTO中,您不需要从应答DTO返回对问题的引用。 你应该做的,简单地说:

  • 将JSON映射到DTO
  • 将DTO映射到实体
    • 在每个答案
    • 中设置问题实体
    • 将答案添加到问题模型
    • 中的答案列表中

    希望这能帮上忙。