When I do
GET /survey/1
I get this response, althought there are questions and answers in my database:
{
"surveyId": 1,
"name": "Example",
"questions": null,
"answers": null
}
Why I get null in 'questions' and 'answers'? How could I fix it?
SurveyRepository:
public interface SurveyRepository extends CrudRepository<Survey, Integer> { }
Survey's model class:
@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's controller class:
@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";
}
}
Answer model class:
@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;
}
}
Question model class
@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;
}
}