Java 邮递员中外键的post数据

Java 邮递员中外键的post数据,java,spring,spring-boot,spring-data-jpa,postman,Java,Spring,Spring Boot,Spring Data Jpa,Postman,这是我的模型类。有来自另一个表AssessmentProperties的外键。现在,当我用postman发布数据时,我的数据如下所示 public class SectionProperties { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long section_id; @Column( columnDefinition = "bigint default 0&q

这是我的模型类。有来自另一个表AssessmentProperties的外键。现在,当我用postman发布数据时,我的数据如下所示

public class SectionProperties {
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long section_id;
    @Column( columnDefinition = "bigint default 0")
    private Long section_no;
    @ManyToOne
    @JoinColumn(name="assessment_id")
    @OnDelete(action = OnDeleteAction.CASCADE)
    private AssesmentProperties foreign_key;
    private String section_type;
    private int weightage;
    private int time;
    private int No_of_questions;
    //getters and setters
}
"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
"foreign_key":{
    "assessment_id":1
}
但我的输入应该是这样的

public class SectionProperties {
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long section_id;
    @Column( columnDefinition = "bigint default 0")
    private Long section_no;
    @ManyToOne
    @JoinColumn(name="assessment_id")
    @OnDelete(action = OnDeleteAction.CASCADE)
    private AssesmentProperties foreign_key;
    private String section_type;
    private int weightage;
    private int time;
    private int No_of_questions;
    //getters and setters
}
"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
"foreign_key":{
    "assessment_id":1
}
谁能告诉我我该怎么做? 顺便说一下,这是post方法的控制器

"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
 "assessment_id":1
@RequestMapping(value=“/SectionProperty”,method=RequestMethod.POST)
公共响应属性createOrUpdateoptions(@RequestBody SectionProperties模型)
{
SectionProperties updated=properties.createOrUpdateSections(模型);
返回新的ResponseEntity(更新的.getSection_id()、新的HttpHeaders()、HttpStatus.OK);
}

不要将SectionProperties用作
@RequestBody
参数,而是创建一个自定义DTO(DataTransferObject)类,该类在JSON格式中如下所示:

@RequestMapping(value="/SectionProperty",method=RequestMethod.POST)
public ResponseEntity<Long> createOrUpdateoptions(@RequestBody SectionProperties model)
{
    SectionProperties updated=properties.createOrUpdateSections(model);
    return new ResponseEntity<Long>(updated.getSection_id(),new HttpHeaders(),HttpStatus.OK);
}
就像在波乔这样:

"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
"assessment_id":1
然后,您的方法应该如下所示,请注意,您必须更改逻辑以将DTO对象转换为实体,反之亦然:

public class SectionPropertiesDTO {
    private int assessment_id;
    private String section_type;
    private int weightage;
    private int time;
    private int no_of_questions;

    //getters and setters
}
@RequestMapping(value=“/SectionProperty”,method=RequestMethod.POST)
公共响应属性createOrUpdateoptions(@RequestBody SectionPropertiesDTO模型)
{
//TODO:将createOrUpdateSections更改为从DTO转换为实体;
SectionProperties updated=properties.createOrUpdateSections(模型);
返回新的ResponseEntity(更新的.getSection_id()、新的HttpHeaders()、HttpStatus.OK);
}

如何生成该post数据?就JSON转换而言,它看起来是正确的。因为您有一个名为foreign_key的对象,其类型为assessmentproperties,并且可能只有联接列(assessment_id)对于Hibernate来说就足够了。在postman中,我像第一个输入一样发布数据,工作正常。我想发布第二个输入,但不使用实体类作为模型类。创建您自己的模型,其中包含所需的特性。将实体类用作域模型不是一个好主意。好的,我明白了,谢谢