【问题标题】:spring input validation with put request使用 put 请求进行弹簧输入验证
【发布时间】: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


【解决方案1】:

您需要在您的实体中添加 ID 以保存具有该 ID 的实体作为参考。

很容易解决:

Question newQuestionEntity = convertToEntity(questionDTO);
newQuestionEntity.setId(testID);

最好的解决方案是:

questionDTO.setID(testID);
public Question convertToEntity(QuestionDTO qDto) {
       Question question = new Question(qDto.getID()) ;
        ....... 
        return question;
} 

【讨论】:

  • 我不明白问题到底出在哪里?是问题实体还是测试实体,我需要设置哪一个?
  • 您的问题是您在保存之前没有添加实体ID,当您更新元素时​​,您需要添加将被更新的实体ID。
  • 我需要设置哪个id?测试 id 或问题 id ?因为我确实尝试设置两者但它没有工作
  • 没关系,问题是我没有检查空字段
  • 如果它的错误已经解决,否则只需给我发个下午,我会给你我到目前为止的代码
【解决方案2】:

当您尝试使用空 id 保存实体关联时会发生此错误。 意思是这种情况下convertToEntity方法在 Question newQuestionEntity = convertToEntity(questionDTO);

用新的Test 对象返回 questionEntity。 当 id 为 null 时,您应该检查实体内的所有关系是否为 null。

【讨论】:

  • 很抱歉,我不明白我该如何解决这个问题,请您详细说明一下吗?
猜你喜欢
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 2023-03-24
  • 2020-06-21
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多