Java 如何将数据传递到外键,同时使用postman将数据传递为null?

Java 如何将数据传递到外键,同时使用postman将数据传递为null?,java,spring-boot,orm,mapping,one-to-many,Java,Spring Boot,Orm,Mapping,One To Many,我试图用外键传递数据,但得到的是空值。请参见下面我的代码: 这是pojo类的功能: @Data @AllArgsConstructor @NoArgsConstructor @ApiModel(value = "Feature", description = "This class is used to specify the Feature Property") @Entity public class Feature { @ApiModelProperty(notes = "uniq

我试图用外键传递数据,但得到的是空值。请参见下面我的代码:

这是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;
}
这就是波乔的计划

@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;
我在所有外键字段中得到空值

{
    "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
}
这是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;
    }
}
这是PlanImpl类

@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;
        }
}
我想显示一个具有多个功能的计划,但它返回null。
谢谢大家!

您需要将功能和模板添加到计划中。大概是这样的:

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);
   }
}
添加后,只需在保存计划之前调用addFeature和模板setter。您甚至可以调整它以一次保存所有功能

这里有一个很好的关于如何映射@OneToMany和@ManyToMany的源代码 和

您需要将功能和模板添加到计划中。大概是这样的:

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);
   }
}
添加后,只需在保存计划之前调用addFeature和模板setter。您甚至可以调整它以一次保存所有功能

这里有一个很好的关于如何映射@OneToMany和@ManyToMany的源代码 和

您还没有发布DOT的代码。您还没有发布planImpl.updatePlan()的代码。所以我们无法提供帮助。@JBNizet我已经添加了planImpl类,感谢您的响应。在JSON中,您有(例如)
“功能”:[{“planId”:1}]
。此JSON映射到PlanDTO。PlanDTO确实有一个名为
features
的属性。这是一套。但是
功能中没有
planId
属性。所以Jackson不能用planId:1做任何事情:它可能在哪里存储这些信息?不要在DTO中使用实体。根据您真正应该收到的内容,将DTO设计为在您的服务中用作参数。请检查您的数据库中是否有计划和功能、模板等之间的映射数据。@gnanajeyam95映射列可用,但具有空值。您尚未发布DOT代码。您还没有发布planImpl.updatePlan()的代码。所以我们无法提供帮助。@JBNizet我已经添加了planImpl类,感谢您的响应。在JSON中,您有(例如)
“功能”:[{“planId”:1}]
。此JSON映射到PlanDTO。PlanDTO确实有一个名为
features
的属性。这是一套。但是
功能中没有
planId
属性。所以Jackson不能用planId:1做任何事情:它可能在哪里存储这些信息?不要在DTO中使用实体。根据您真正应该收到的内容,将DTO设计为在服务中用作参数。请检查数据库中是否有可用的计划与功能、模板等之间的映射数据。@gnanajeyam95映射列可用,但具有空值。此```方法setPlan(plan)存在问题类型集“”的未定义方法setPlan(Plan)对于类型集“”的未定义方法setPlan(Plan)存在问题```
@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;
    }

}
@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;
        }
}
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);
   }
}