我正在使用Spring MVC和Hibernate4开发我的应用程序。我有2个具有相关性的类OneToMany:Lot
可以有许多Question
。在我的singleLot.jsp
中,我只想显示指定Questions
的所有Lot
。当我调试应用程序时,我可以看到从数据库中很好地提取了所有内容,但我无法弄清楚如何在我的singleLot.jsp
页面中显示它。
我认为问题可能是Questions
并不像我在我的实体类中声明的那样直接在我的Lot
对象中作为List<Question>
,但是它们在一个名为a的内部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>
有谁知道我做错了什么?
答案 0 :(得分:1)
应该是
<h5>${question.content}</h5>