【发布时间】:2023-03-21 11:32:01
【问题描述】:
大家好,
我有这个我正在构建的 spring rest api,目前我的控制器上的 put 方法有问题。
我有一个与测试实体有关系的问题实体:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name="question_id", nullable = false, updatable = false)
private Long id;
@Column(name="question_text", nullable = false)
@NotNull
private String question;
@Column(name="question_weight", nullable = false)
@Min(1)
private Integer weight = 1;
@Column(name="question_type", nullable = false)
private String type = "radio";
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_modified", nullable = false)
@LastModifiedDate
private Date lastModified;
@ManyToOne(fetch = FetchType.LAZY, optional = false, targetEntity = com.QCMGenerator.QCMGenerator.Model.Test.class)
@JoinColumn(name = "test_id", referencedColumnName = "test_id", nullable = false, updatable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Test test;
我之前发帖询问过这个问题,我被告知要使用 DTO,所以我这样做了,这是我的问题 DTO:
private Long id;
private String question;
private String type;
private Integer weight;
private Date lastModified;
private TestDTO test;
这是我在控制器中的 put 方法:
@PutMapping("/{questionID}")
public QuestionDTO updateQuestion(
@PathVariable(value = "testID") Long testID,
@PathVariable(value = "questionID") Long questionID,
@Valid @RequestBody QuestionDTO newQuestion
){
if(!testRepo.existsById(testID)){
throw new ResourceNotFoundException("No test with the ID '"+testID+"' was found...");
}
QuestionDTO savedDTO = null;
try {
Question questionEntity = questionRepo.findById(questionID).get();
QuestionDTO questionDTO = convertToDTO(questionEntity);
if (newQuestion.getTest() != null) {
questionDTO.setTest(newQuestion.getTest());
}
if (newQuestion.getQuestion() != null) {
questionDTO.setQuestion(newQuestion.getQuestion());
}
if (newQuestion.getType() != null) {
questionDTO.setType(newQuestion.getType());
}
if (newQuestion.getWeight() != null) {
questionDTO.setWeight(newQuestion.getWeight());
}
Question newQuestionEntity = convertToEntity(questionDTO);
Question saved = questionRepo.save(newQuestionEntity);
savedDTO = convertToDTO(saved);
}catch (Exception e){
System.out.println(e.getMessage());
}
return savedDTO;
}
我在我的 IDE 控制台上不断收到此错误:
2018-11-18 21:33:12.249 WARN 12876 --- [nio-8080-exec-2] ohaiUnresolvedEntityInsertActions:HHH000437:尝试保存一个或多个与未保存的瞬态具有不可为空关联的实体实体。在保存这些依赖实体之前,必须将未保存的瞬态实体保存在操作中。 未保存的瞬态实体:([com.QCMGenerator.QCMGenerator.Model.Test#]) 依赖实体:([[com.QCMGenerator.QCMGenerator.Model.Question#10]]) 不可为空的关联:([com.QCMGenerator.QCMGenerator.Model.Question.test]) org.hibernate.TransientPropertyValueException:非空属性引用瞬态值 - 瞬态实例必须在当前操作之前保存:com.QCMGenerator.QCMGenerator.Model.Question.test -> com.QCMGenerator.QCMGenerator.Model.Test;嵌套异常是 java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: 非空属性引用了一个瞬态值 - 瞬态实例必须在当前操作之前保存:com.QCMGenerator.QCMGenerator.Model.Question.test -> com.QCMGenerator。 QCMGenerator.Model.Test
我希望这里有人能为我澄清这个问题,因为我整天都被困在那个单一的方法或其他方法工作正常,我尝试过使用和不使用 DTO,在添加它之前我有一个在某些字段上接受空值的方法存在问题。
我非常感谢所提供的任何帮助,并感谢大家的帮助。
祝大家有美好的一天。 :D
【问题讨论】:
-
尝试在eclipse中调试put请求并检查值
标签: spring hibernate rest spring-boot put