回应无效

时间:2017-01-19 20:44:40

标签: java spring rest

当我这样做时

GET /survey/1

我得到了这个回复,虽然我的数据库中有问题和答案:

{
  "surveyId": 1,
  "name": "Example",
  "questions": null,
  "answers": null
}

为什么我在'问题'和'答案'中变为空?我怎么能解决它?

SurveyRepository:

public interface SurveyRepository extends CrudRepository<Survey, Integer> { }

Survey的模型类:

@Entity
@Table(name = "survey")
public class Survey {

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

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

    @Transient
    private List<String> questions;

    @Transient
    private List<String> answers;

    public Survey() { }

    public Survey(Integer surveyId, String name) {
        this.surveyId = surveyId;
        this.name = name;
    }

    public Integer getSurveyId() {
        return surveyId;
    }

    public String getName() {
        return name;
    }

    public List<String> getQuestions() {
        return questions;
    }

    public List<String> getAnswers() {
        return answers;
    }

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

    public void setName(String name) {
        this.name = name;
    }

    public void setQuestions(List<String> questions) {
        this.questions = questions;
    }

    public void setAnswers(List<String> answers) {
        this.answers = answers;
    }
}

Survey的控制器类:

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

2 个答案:

答案 0 :(得分:3)

您将其标记为@Transient,这取决于您使用哪一个,这意味着它不会被序列化,或者无法存储在数据库中。请参阅this answer.此外,无关的您应该对@ElementCollection(targetClass = String.class)

使用Lists

答案 1 :(得分:0)

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

  

Java中的OneToMany关系是源对象所具有的关系   存储目标对象集合的属性以及这些属性   目标对象具有与源对象相反的反向关系   这将是一个ManyToOne关系。 Java和Java中的所有关系   JPA是单向的,因为如果源对象引用目标   对象不能保证目标对象也有   与源对象的关系。这与关系不同   数据库,其中关系是通过外键定义的   查询,以便反向查询始终存在。

     

JPA还定义了ManyToMany关系,类似于a   除了反向关系之外的OneToMany关系(如果它   已定义)是ManyToMany关系。主要区别   在JPA中的OneToMany和ManyToMany关系之间是a   ManyToMany总是使用中间关系连接表   存储关系,而OneToMany可以使用连接   表,或引用目标对象的表中的外键   源对象表的主键。如果OneToMany使用外键   在目标对象的表中,JPA要求关系为   双向的(逆MultiToOne关系必须在。中定义)   目标对象),源对象必须使用mappedBy属性   定义映射。

阅读更多内容JPA Persistence和此tutorial Spring