【问题标题】:JPA avoid sending id when creating the objectJPA 在创建对象时避免发送 id
【发布时间】:2019-03-01 17:02:37
【问题描述】:

我正在尝试通过 json 发送一个调用我的端点的问题,但他向我询问 id,我不知道该做什么来避免它,我读到了 @JsonIgnore,但我不知道那是不是这样做的方法。

这是我的模型:

   @Entity(name = "question")
   public class Question extends DateAudit {
       @Id
       @Column(name = "question_id")
       @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_seq")
       @SequenceGenerator(name = "question_seq", allocationSize = 1)
       private Long id;

       @Column(name = "text_question")
       private String textQuestion;

       @Column(name = "answer_description")
       @NotBlank(message = "Answer description")
       private String answer_description;

       @Column(name = "is_exam_question", nullable = false)
       private Boolean is_exam_question;

       @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
       private Set<Answer> answers;

       @Column(name = "answer_type", nullable = false)
       private String answerType;

       public Question(String text_question, String answer_description, String answerType, Set<Answer> answerSet, Boolean is_exam_question) {
           super();
           this.textQuestion = text_question;
           this.answer_description = answer_description;
           this.answers = answerSet;
           this.answerType = answerType;
           this.is_exam_question = is_exam_question;
       }

       //Getters
   }

这是我的终点

@PostMapping("/create")
       @PreAuthorize("hasRole('ADMIN')")
       @ApiOperation(value = "Allows the teacher to create a question and add it to the repository")
       public ResponseEntity<?> createQuestion(@Valid @RequestBody Question questionRequest) {

           if (questionService.existQuestion(questionRequest) != null) {
               //Question already exists
               return ResponseEntity.ok(new ApiResponse("Question already exist on your repository", false));
           } else {
               questionService.create(
                       questionRequest.getText_question(),
                       questionRequest.getAnswer_description(),
                       questionRequest.getAnswerType(),
                       questionRequest.getAnswers(),
                       questionRequest.getIs_exam_question()
               );

           }
           return ResponseEntity.ok(new ApiResponse("Question added successfully", true));
       }

我大摇大摆,他要求这些价值观:

    {
     "answerType": "string",
     "answer_description": "string",
     "answers": [
       {
         "answer_to_question": "string",
         "createdAt": "2019-03-01T16:44:05.102Z",
         "id": 0,
         "updatedAt": "2019-03-01T16:44:05.102Z"
       }
     ],
     "createdAt": "2019-03-01T16:44:05.102Z",
     "id": 0,
     "is_exam_question": true,
     "text_question": "string",
     "updatedAt": "2019-03-01T16:44:05.102Z"
   }

如何在不添加这些 ID 的情况下创建问题?但是当收到问题时,我想获得 id。

【问题讨论】:

    标签: java spring spring-boot jpa


    【解决方案1】:

    如果您只想从 @ApiModelProperty(hidden=true) 大摇大摆的注释字段中排除属性

    【讨论】:

    • 有了这个,我会在执行 Get 时获得 Id,而当我执行 put 时,我不会将其添加到 json 正文中?
    • @StuartDTO 我误解了一个问题,您是否尝试在 id 字段中添加“@ApiModelProperty(hidden=true)”?
    • 不要大摇大摆,这是一个很好的观点,我认为我不知道这一点,但我想创建一个问题而不添加 Id 作为参数,因为它是自动增量的,所以我不知道编号...并且分配问题的答案也做得不好,因为我不知道在尚未创建它时如何分配它,我的意思是流程是同时创建问题和答案跨度>
    • 好吧,从 REST 客户端不要传递 'id' 字段或传递 null,如果你想从 swagger 中发布帖子,你也必须排除 'id'。
    • 您能否提供一个示例,说明如何从客户端发布新问题?不创建客户端,JSON 应该如何?或者我怎么知道它正在检测的 json?如果 id 是自动递增的,我不能将它们作为 json 值传递
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2018-09-02
    • 2019-01-08
    相关资源
    最近更新 更多