JPA避免在创建对象时发送ID

时间:2019-03-01 17:02:38

标签: java spring spring-boot jpa

我正在尝试通过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。

1 个答案:

答案 0 :(得分:0)

如果您只是想从招摇中注释掉属性@apiModelProperty(hidden = true)