Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 响应为空_Java_Spring_Rest - Fatal编程技术网

Java 响应为空

Java 响应为空,java,spring,rest,Java,Spring,Rest,当我这样做的时候 GET /survey/1 虽然我的数据库中有问题和答案,但我得到了这样的回答: { "surveyId": 1, "name": "Example", "questions": null, "answers": null } 为什么我在“问题”和“答案”中为空?我怎样才能修好它 调查报告: public interface SurveyRepository extends CrudRepository<Survey, Integer> { }

当我这样做的时候

GET /survey/1
虽然我的数据库中有问题和答案,但我得到了这样的回答:

{
  "surveyId": 1,
  "name": "Example",
  "questions": null,
  "answers": null
}
为什么我在“问题”和“答案”中为空?我怎样才能修好它

调查报告:

public interface SurveyRepository extends CrudRepository<Survey, Integer> { }
问题模型课

@Entity
@Table(name = "question")
public class Question {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "questionId")
    private Integer questionId;

    @Column(name = "question", nullable = false)
    private String question;

    @ManyToOne
    @JoinColumn(name = "surveyId", nullable = false)
    private Survey surveyId;

    @Transient
    private List<String> answers;

    public Question() { }

    public Question(Integer questionId, String question, Survey surveyId) {
        this.questionId = questionId;
        this.question = question;
        this.surveyId = surveyId;
    }

    public Integer getQuestionId() {
        return questionId;
    }

    public String getQuestion() {
        return question;
    }

    public Survey getSurveyId() {
        return surveyId;
    }

    public void setQuestionId(Integer questionId) {
        this.questionId = questionId;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public void setSurveyId(Survey surveyId) {
        this.surveyId = surveyId;
    }
}
@实体
@表(name=“问题”)
公开课问题{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“questionId”)
私有整数问号;
@列(name=“question”,nullable=false)
私有字符串问题;
@许多酮
@JoinColumn(name=“surveyId”,nullable=false)
私人调查;
@短暂的
私人名单答案;
公共问题(){}
公共问题(整数问题ID、字符串问题、调查问卷ID){
this.questionId=questionId;
这个问题=问题;
this.surveyId=surveyId;
}
公共整数getQuestionId(){
返回问题ID;
}
公共字符串getQuestion(){
返回问题;
}
公众调查getSurveyId(){
退货调查;
}
public void setQuestionId(整数questionId){
this.questionId=questionId;
}
公共问题(字符串问题){
这个问题=问题;
}
公共空间设置调查(调查调查){
this.surveyId=surveyId;
}
}

您与您的调查和问题没有任何关系

Java中的OneToMany关系是源对象具有 属性,该属性存储目标对象的集合,如果 目标对象与源对象具有反向关系 这将是一种多人的关系。Java和Java中的所有关系 JPA是单向的,因为如果源对象引用目标对象 对象不能保证目标对象也具有 与源对象的关系。这与关系数据库不同 数据库,其中关系通过外键和 以使反向查询始终存在的方式进行查询

JPA还定义了多人关系,这类似于 一对一关系,但反向关系除外(如果 是一种多人关系。主要区别 在JPA中,一对夫妻和多对夫妻之间的关系是 ManyToMany始终使用中间关系联接表 存储关系,而OneToMany可以使用连接 表,或目标对象表中引用 源对象表的主键。如果OneToMany使用外键 在目标对象的表中,JPA要求 必须在中定义双向(反向)多工单体关系 目标对象),并且源对象必须使用mappedBy属性 来定义映射


阅读更多信息,您与您的调查和问题没有任何关系

Java中的OneToMany关系是源对象具有 属性,该属性存储目标对象的集合,如果 目标对象与源对象具有反向关系 这将是一种多人的关系。Java和Java中的所有关系 JPA是单向的,因为如果源对象引用目标对象 对象不能保证目标对象也具有 与源对象的关系。这与关系数据库不同 数据库,其中关系通过外键和 以使反向查询始终存在的方式进行查询

JPA还定义了多人关系,这类似于 一对一关系,但反向关系除外(如果 是一种多人关系。主要区别 在JPA中,一对夫妻和多对夫妻之间的关系是 ManyToMany始终使用中间关系联接表 存储关系,而OneToMany可以使用连接 表,或目标对象表中引用 源对象表的主键。如果OneToMany使用外键 在目标对象的表中,JPA要求 必须在中定义双向(反向)多工单体关系 目标对象),并且源对象必须使用mappedBy属性 来定义映射


阅读更多内容,您将它们标记为
@Transient
,这取决于您使用的是哪一个,这意味着它不会被序列化,或者不会存储在数据库中。另请参见,您可能应该在那些
列表上使用
@ElementCollection(targetClass=String.class)
,您将它们标记为
@Transient
,这取决于您使用的是哪一个,这意味着它不会被序列化,或者不会存储在数据库中。另请参见,您可能应该在那些
列表上使用
@ElementCollection(targetClass=String.class)

我编辑了我的帖子并添加了问答模型类-我在那里使用了@ManyToOne注释。我编辑了帖子并添加了问答模型类-我在那里使用了@ManyToOne注释。
@RestController
@RequestMapping("/survey")
public class SurveyController {

    @Autowired
    private SurveyRepository surveyRepo;

    @Autowired
    private AnswerRepository answerRepo;

    @Autowired
    private QuestionRepository questionRepo;

    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public Survey getSurveyById(@PathVariable("id") int id) {
        return surveyRepo.findOne(id);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String create(@RequestBody Survey survey) {
        surveyRepo.save(survey);
        return "Survey created";
    }

    @RequestMapping(method = RequestMethod.GET)
    public Iterable<Survey> getAllSurveys() {
        return surveyRepo.findAll();
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public String delete(@PathVariable("id") int id) {
        surveyRepo.delete(id);
        return "Survey deleted";
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
    public String update(@PathVariable("id") int id, @RequestBody Survey survey) {
        Survey update = surveyRepo.findOne(id);

        update.setName(survey.getName());
        update.setQuestions(survey.getQuestions());

        surveyRepo.save(update);
        return "Survey updated";
    }

}
@Entity
@Table(name = "answer")
public class Answer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "answerId")
    private Integer answerId;

    @Column(name = "answer", nullable = false)
    private String answer;

    @ManyToOne
    @JoinColumn(name = "questionId", nullable = false)
    private Question questionId;

    public Answer() { }

    public Answer(Integer answerId, String answer, Question questionId) {
        this.answerId = answerId;
        this.answer = answer;
        this.questionId = questionId;
    }

    public Integer getAnswerId() {
        return answerId;
    }

    public String getAnswer() {
        return answer;
    }

    public Question getQuestionId() {
        return questionId;
    }

    public void setAnswerId(Integer answerId) {
        this.answerId = answerId;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public void setQuestionId(Question questionId) {
        this.questionId = questionId;
    }

}
@Entity
@Table(name = "question")
public class Question {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "questionId")
    private Integer questionId;

    @Column(name = "question", nullable = false)
    private String question;

    @ManyToOne
    @JoinColumn(name = "surveyId", nullable = false)
    private Survey surveyId;

    @Transient
    private List<String> answers;

    public Question() { }

    public Question(Integer questionId, String question, Survey surveyId) {
        this.questionId = questionId;
        this.question = question;
        this.surveyId = surveyId;
    }

    public Integer getQuestionId() {
        return questionId;
    }

    public String getQuestion() {
        return question;
    }

    public Survey getSurveyId() {
        return surveyId;
    }

    public void setQuestionId(Integer questionId) {
        this.questionId = questionId;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public void setSurveyId(Survey surveyId) {
        this.surveyId = surveyId;
    }
}