Hibernate4 で Spring MVC を使用してアプリケーションを開発しています。関連 OneToMany: Lot
can have many を持つ2つのクラスがありますQuestion
。私の中では、指定されたsingleLot.jsp
すべてを表示したいだけです。アプリケーションをデバッグすると、データベースからすべてが適切に抽出されていることがわかりますが、ページに表示する方法がわかりません。Questions
Lot
singleLot.jsp
問題は、エンティティ クラスで宣言したように、オブジェクトQuestions
内に a として直接含まれていない可能性があると思いますが、 aと呼ばれるもの内にあります。Lot
List<Question>
PersistentBag
私のLot
エンティティ:
@Entity
@Table(name = "lot")
public class Lot {
private Long lotId;
private String name;
private String description;
private Timestamp dateAdded;
private List<Question> questions = new ArrayList<Question>(0); //One to many relation
@Id
@Column(name = "lot_id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getLotId(){ return lotId; }
public void setLotId(Long id) { this.lotId = id; }
@NotNull
@Length(min = 1, max = 50)
@Column(name = "name")
public String getName() {return name;}
public void setName(String name) {this.name = name;}
@NotNull
@Length(min = 1, max = 500)
@Column(name = "description")
public String getDescription() {return description;}
public void setDescription(String description) {this.description = description;}
@Column(name = "date_added")
public Timestamp getDateAdded() {return dateAdded;}
public void setDateAdded(Timestamp dateAdded) {this.dateAdded = dateAdded;}
@OneToMany(mappedBy = "lotId", fetch = FetchType.EAGER )
public List<Question> getQuestions() {return questions;}
public void setQuestions(List<Question> questions) {this.questions = questions;}
}
私のQuestion
エンティティ:
@Entity
@Table(name = "question")
public class Question {
private Long questionId;
private Long lotId;
private String content;
private List<Answer> answer = new ArrayList<Answer>(0); //One to many relation
@Id
@Column(name = "question_id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getQuestionId(){ return questionId; }
public void setQuestionId(Long id) { this.questionId = id; }
@NotNull
@Length(min = 1, max = 10)
@Column(name = "lot_id")
public Long getLotId(){return lotId;}
public void setLotId(Long lotId) { this.lotId = lotId; }
@NotNull
@Length(min = 1, max = 5000)
@Column(name = "content")
public String getContent(){return content;}
public void setContent(String content) { this.content = content; }
@OneToMany(mappedBy = "questionId", fetch = FetchType.LAZY )
public List<Answer> getAnswer() {return answer;}
public void setAnswer(List<Answer> answers) {this.answer = answers;}
@Override
public String toString() {
return "Question nr. [" + questionId + "] - " + content + "and "+ getAnswer().size() + " possible answers.";
}
}
LotController
の方法:
@RequestMapping(value = "{lotId}", method = RequestMethod.GET)
public ModelAndView viewSingleLot(@PathVariable Long lotId){
ModelAndView modelAndView;
Lot lot = lotService.getLot(lotId);
modelAndView = new ModelAndView("singleLot");
modelAndView.addObject("lot", lot);
return modelAndView;
}
singleLot.jsp
そして、コントローラーで提供されたデータにアクセスしようとしている私の断片:
<c:forEach items="${lot.questions}" var="question" varStatus="rowCounter">
<div >
<div>
<h3>${rowCounter}</h3>
</div>
<div>
<h5>question.name</h5>
</div>
</c:forEach>
私が間違っていることを誰かが知っていますか?