0

我正在使用带有 Hibernate4 的 Spring MVC 开发我的应用程序。我有 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>

有谁知道我做错了什么?

4

1 回答 1

1

它应该是

<h5>${question.content}</h5>
于 2013-07-28T23:13:38.467 回答