[英]How to pass data into a foreign key , while passing data i am getting null using postman?
I am trying to pass data in foreign key but i am getting null value. 我试图通过外键传递数据,但我得到的是空值。 See below my code:
请参阅下面的代码:
This is the feature pojo class: 这是pojo功能类:
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "Feature", description = "This class is used to specify the Feature Property")
@Entity
public class Feature {
@ApiModelProperty(notes = "unique feature id")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long featureId;
@NotNull
@ApiModelProperty(notes = " feature name will define name of the feature")
@Size(max = 50)
private String name;
@NotEmpty(message = "feature version should never be empty")
@Size(max = 20)
@ApiModelProperty(notes = "feature version will define version of the feature")
@Column(unique = true)
private String version;
@NotBlank
@Size(max = 500)
@ApiModelProperty(notes = "feature description will describe the feature")
private String description;
@ManyToOne
@JoinColumn(name = "plan_id")
private Plan plan;
}
This is the plan pojo 这是计画
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "PlanData", description = "Plan is a pojo class under plan management")
@Entity
public class Plan {
@ApiModelProperty(notes = "unique plan id")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Plan_Id")
private Long planId;
@ApiModelProperty(notes = "A plan name")
private String planName;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
@DateTimeFormat
@ApiModelProperty(notes = "plan start date will defines from what date the plan is active ")
private Date planStartDate;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
@DateTimeFormat
@ApiModelProperty(notes = "plan end date will define till what date the plan will active ")
private Date planEndDate;
@ApiModelProperty(notes = "planDescription defines about the plan in detail")
private String planDescription;
@ApiModelProperty(notes = "plan version specifies version sepecific knowledge")
private String planVersion;
@ApiModelProperty(notes = "planStatus defines the active/deactive status of plan")
@JsonProperty
@Column(nullable = false, columnDefinition = "BOOLEAN")
private Boolean planStatus = true;
@OneToMany(mappedBy = "plan", cascade = CascadeType.ALL)
private Set<Feature> features=new HashSet<Feature>();;
@OneToOne
@JoinColumn(name = "templateId")
@ApiModelProperty(notes = "This property is mainly use to bind with the template")
private Template template;
@OneToOne
@JoinColumn(name = "termsId")
@ApiModelProperty(notes = "This property is mainly use to bind with the terms")
private TermsAndConditions terms;
This is post api for plan 这是发布计划的API
@PostMapping(value = "/plan")
public ResponseEntity<PlanDTO> createPlan(@RequestBody PlanDTO planDTO ) {
PlanDTO plan = planImpl.createPlan(planDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(plan);
}
This is post api for feature 这是发布api的功能
@ApiOperation("This api will use to create a feature data ")
@PostMapping(value = "/feature")
public ResponseEntity<FeatureDTO> createFeature(@RequestBody FeatureDTO featureDTO) {
FeatureDTO feature = featureImpl.createFeature(featureDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(feature);
}
This Iam sending through the postman request: 该Iam通过邮递员请求发送:
{
"planName": "bnvn",
"planStartDate": "2003-09-09",
"planEndDate": "2003-09-09",
"planDescription": "hvxncbvnc jfbvfd nbhnsdf cbc",
"planVersion": "cvnm mxcnvbnc xncv",
"planStatus": true,
"features": [{"planId":1
}],
"template": {
"templateId" :1
},
"terms": {
"termsId" :1
}
}
I am getting null values in all the foreign key field. 我在所有外键字段中都得到了空值。
{
"planId": 3,
"planName": "bnvn",
"planStartDate": "2003-09-09T00:00:00.000+0000",
"planEndDate": "2003-09-09T00:00:00.000+0000",
"planDescription": "hvxncbvnc jfbvfd nbhnsdf cbc",
"planVersion": "cvnm mxcnvbnc xncv",
"planStatus": true,
"features": [],
"template": null,
"terms": null
}
This is the plan dto 这是计划dto
@Data
public class PlanDTO {
private Long planId;
private String planName;
private Date planStartDate;
private Date planEndDate;
private String planDescription;
private String planVersion;
private Boolean planStatus;
private Set<Feature> features = new HashSet<Feature>();
private Template template;
private TermsAndConditions terms;
public PlanDTO() {
}
public PlanDTO(Long planId, String planName, Date planStartDate, Date planEndDate, String planDescription,
String planVersion, Boolean planStatus, Set<Feature> features, Template template,
TermsAndConditions terms) {
super();
this.planId = planId;
this.planName = planName;
this.planStartDate = planStartDate;
this.planEndDate = planEndDate;
this.planDescription = planDescription;
this.planVersion = planVersion;
this.planStatus = planStatus;
this.features = features;
this.template = template;
this.terms = terms;
}
}
This is the feature dto: 这是功能dto:
@Data
public class FeatureDTO {
private Long featureId;
private String name;
private String version;
private String Description;
private Plan plan;
public FeatureDTO() {
}
public FeatureDTO(Long featureId, String name, String version, String description, Plan plan) {
super();
this.featureId = featureId;
this.name = name;
this.version = version;
this.Description = description;
this.plan = plan;
}
}
This is the plan impl class 这是计划展示类
@Service
public class PlanImpl {
private static final Logger logger = LoggerFactory.getLogger(PlanImpl.class);
@Autowired
private PlanRepo planRepo;
@Autowired
private PlanMapper planMapper;
/**
* createPlan will create a plan and will store it in repository
*
* @param plan
* @return
*/
public PlanDTO createPlan(PlanDTO planDTO) {
Plan plan = planMapper.toPlan(planDTO);
Plan planData = planRepo.save(plan);
PlanDTO plans = planMapper.toPlanDTO(planData);
return plans;
}
/**
* update plan will update a plan using plan id and store it in database
*
* @param planId
* @param plan
*/
public PlanDTO updatePlan(Long planId, PlanDTO planDTO) {
Plan plan = planMapper.toPlan(planDTO);
Plan updatePlan;
if (planRepo.findById(planId) == null) {
throw new NullPointerException();
} else {
plan.setPlanId(planId);
updatePlan = planRepo.getOne(plan.getPlanId());
updatePlan.setPlanName(plan.getPlanName());
updatePlan.setPlanStartDate(plan.getPlanStartDate());
updatePlan.setPlanEndDate(plan.getPlanEndDate());
updatePlan.setPlanDescription(plan.getPlanDescription());
updatePlan.setPlanVersion(plan.getPlanVersion());
updatePlan.setPlanStatus(plan.getPlanStatus());
Plan planUpdateData = planRepo.save(updatePlan);
PlanDTO Plans = planMapper.toPlanDTO(planUpdateData);
return Plans;
}
}
I want to show one plan with multiple features but it is returning null. 我想显示一个具有多种功能的计划,但它返回null。 Thank you !
谢谢 !
You need to add the features and the template to the plan. 您需要将特征和模板添加到计划中。 Something like this:
像这样:
public class Plan {
...
public void addFeature(Feature feature) {
this.features.add(feature);
feature.setPlan(this);
}
public void setTemplate(Template template) {
this.template = template;
template.setPlan(this);
}
}
After adding, just call the addFeature and template setter before saving the plan. 添加后,只需在保存计划之前调用addFeature和模板设置器即可。 You could even adapt it to save all features at once.
您甚至可以修改它以一次保存所有功能。
Here is a good source on how to map with @OneToMany and @ManyToMany https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/ and https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/ 这是有关如何使用@OneToMany和@ManyToMany https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/和https进行映射的好资源: //vladmihalcea.com/与jpa-and-hibernate关联的最佳方式映射一个onetoone关系/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.