Spring のサービス レイヤーからエンティティのリストを返すと、Tomcat からエラー 500 が発生し続けます。メモリ内でその場でエンティティを作成するだけでは、エラーは発生せず、正常に返されるため、特にトランザクションと関係があります。
コントローラーに次のメソッドがあります...
@RequestMapping(value = "{examid}", method = RequestMethod.GET)
@ResponseBody
public List<Exam> getExam(@PathVariable String examid, Model model) {
return examService.getAllExams();
}
このメソッドが呼び出されると、Tomcat はエラー 500 を表示し、スタック トレースはまったく表示しません。私はそれをtry catchでラップしても、そのスタックトレースには何も表示されません。
このサービスは、dao を呼び出すだけです...
@Service("examService")
public class ExamServiceImpl implements ExamService{
@Autowired
private ExamDao examDao;
@Override
@Transactional(readOnly = true)
public List<Exam> getAllExams() {
return examDao.getAllExams();
}
そして、この dao は単純な HQL スクリプトを返すだけです...
@Repository("examDao")
public class ExamDaoImpl implements ExamDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Exam> getAllExams() {
return sessionFactory.getCurrentSession().createQuery("from Exam")
.list();
}
もともと、エンティティが遅延ロードされていることに関係していると思っていました。私のエンティティはご覧のとおり下にあります。非常に単純です。
@Entity
@Table(name = "exams")
public class Exam implements Serializable {
private static final long serialVersionUID = -5109075534794721930L;
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name = "examid", unique = true)
private String examId;
@OneToMany(mappedBy = "exam")
private Set<Question> questions;
public String getExamId() {
return examId;
}
public void setExamId(String examId) {
this.examId = examId;
}
/**
* @return the questions
*/
@JsonManagedReference
public Set<Question> getQuestions() {
return questions;
}
/**
* @param questions the questions to set
*/
public void setQuestions(Set<Question> questions) {
this.questions = questions;
}
}
質問エンティティ
@Entity
@Table(name = "questions")
public class Question implements Serializable {
private static final long serialVersionUID = 8804841647267857850L;
private String questionId;
private Exam exam;
private Set<Answer> answers;
/**
* @return the answer
*/
@JsonManagedReference
@OneToMany(mappedBy = "question")
public Set<Answer> getAnswers() {
return answers;
}
/**
* @param answer
* the answer to set
*/
public void setAnswers(Set<Answer> answers) {
this.answers = answers;
}
/**
* @return the exam
*/
@JsonBackReference
@ManyToOne
@JoinColumn(name = "examid", nullable = false)
public Exam getExam() {
return exam;
}
/**
* @param exam
* the exam to set
*/
public void setExam(Exam exam) {
this.exam = exam;
}
/**
* @return the questionId
*/
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "questionid", unique = true)
public String getQuestionId() {
return questionId;
}
/**
* @param questionId
* the questionId to set
*/
public void setQuestionId(final String questionId) {
this.questionId = questionId;
}
}
回答エンティティ
@Entity
@Table(name = "answers")
public class Answer implements Serializable {
private static final long serialVersionUID = -3922870865886851018L;
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name = "answerid", unique = true)
private String answerId;
@ManyToOne()
@JoinColumn(name = "questionid", nullable = false)
private Question question;
/**
* @return the question
*/
@JsonBackReference
public Question getQuestion() {
return question;
}
/**
* @param question the question to set
*/
public void setQuestion(Question question) {
this.question = question;
}
/**
* @return the answerId
*/
public String getAnswerId() {
return answerId;
}
/**
* @param answerId the answerId to set
*/
public void setAnswerId(final String answerId) {
this.answerId = answerId;
}
}
私はJackson 2.2.0を使用しています - 現在、次のコードは正常に動作するため、結合(元々は遅延ロードされていましたが、デバッグのために削除されました)のいずれかに問題があることは間違いありません。コントローラーから次のコードを返すだけでうまくいきます...
Exam exam = new Exam();
Question presidentQuestion = new Question();
presidentQuestion.setExam(exam);
Question skyQuestion = new Question();
skyQuestion.setExam(exam);
Set<Question> questions = new HashSet<>();
questions.add(skyQuestion);
questions.add(presidentQuestion);
exam.setQuestions(questions);
Answer answer = new Answer();
answer.setQuestion(skyQuestion);
Answer answer1 = new Answer();
answer1.setQuestion(skyQuestion);
Answer answer2 = new Answer();
answer2.setQuestion(presidentQuestion);
Set<Answer> answers1 = new HashSet<>();
answers1.add(answer2);
Set<Answer> answers = new HashSet<>();
answers.add(answer);
answers.add(answer1);
presidentQuestion.setAnswers(answers1);
skyQuestion.setAnswers(answers);
return Arrays.asList(exam);
Tomcat のログには何も記録されていません。何らかのスタックトレースを強制するにはどうすればよいですか? Hibernate バージョン = 4.1.10.Final、Spring バージョン 3.1.4。