微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

已解决 - POST 请求未在数据库表中设置外键值

如何解决已解决 - POST 请求未在数据库表中设置外键值

我正在使用 Spring Data JPA,每当我发送 POST 请求以创建新的 JobQuestionAnswer(下面给出的实体)时,引用 JobQuestion 的外键值不会插入到表中。通过发送带有正文的 POST 请求进行测试:

# plane
planesource = vtk.vtkPlanesource()
planesource.Setorigin(-100.0,-100.0,0.0)
# planesource.Setnormal(0.0,0.0,1.0)
planesource.SetResolution(100,100)
planesource.SetPoint1(100.0,0.0)
planesource.SetPoint2(-100.0,100.0,0.0)
planesource.Update()

plane = planesource.Getoutput()

# Create a mapper and actor
mapperP = vtk.vtkpolyDataMapper()
mapperP.SetInputData(plane)

actorP = vtk.vtkActor()
actorP.SetMapper(mapperP)
actorP.Getproperty().SetColor(0,0)
actorP.Getproperty().EdgeVisibilityOn() # showing mesh
actorP.Getproperty().SetEdgeColor(1,1,1)
actorP.Getproperty().Setopacity(0.2) # transparency
...
renderer.AddActor(actorP)

对问题可能是什么有任何想法吗?

[解决方案]: 啊……简直不敢相信我在这上面花了这么多时间,但我已经设法解决了这个问题。这不是代码,而是 POST 请求正文。为了使其正常工作,需要提供 JobQuestion 对象的链接。 示例:

{
    "answer": "Here is the answer for job question 1","jobQuestion": {
        "id": 1
    }
}

我有两个实体 JobQuestion 和 JobQuestionAnswer 定义为:

{
    "answer": "Here is the answer for job question 1","jobQuestion": "http://localhost:9000/jobquestion/1"
}
@EqualsAndHashCode(callSuper = true)
@Getter
@Setter
@Entity
@Table(name="job_question")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class JobQuestion extends AbstractAuditableEntity<User,Long> implements Serializable {

    @Getter
    @Setter
    private String question;

    @Getter
    @Setter
    private QuestionType questionType;

    @Getter
    @Setter
    @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name="job_test_id")
    private JobTest jobTest;

    @Getter
    @Setter
    @EqualsAndHashCode.Exclude
    @ElementCollection
    private List<String> possibleAnswers = new ArrayList<>();

    @Getter
    @Setter
    @EqualsAndHashCode.Exclude
    @OnetoMany(mappedBy="jobQuestion",cascade = CascadeType.ALL)
    private Set<JobQuestionAnswer> questionAnswers = new HashSet<>();

}

这是我的仓库:

@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@Entity
@Table(name="job_question_answer")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class JobQuestionAnswer extends AbstractAuditableEntity<User,Long> implements Serializable {

    @Getter
    @Setter
    @ManyToOne(cascade = CascadeType.ALL)
    @EqualsAndHashCode.Exclude
    @JoinColumn(name="job_question_id",referencedColumnName = "id")
    private JobQuestion jobQuestion;

    @Getter
    @Setter
    private String answer;

}
@RepositoryRestResource(path = "jobquestion",collectionResourceRel = "jobquestion",itemResourceRel = "jobquestion")
public interface JobQuestionRepository extends JpaRepository<JobQuestion,Long> {

    Optional<JobQuestion> findById(Long id);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。