Posted on 2010-01-13 23:26
斷點 閱讀(401)
評論(0) 編輯 收藏 所屬分類:
Hibernate
有兩張表(question、answer),它們存在著一對多關(guān)系(question->answer)和多對一關(guān)系(answer->question)。
在Answer.java中定義有:
public class Answer {
private String userid;
private Question question;
private int qid;
}
在answer.hbm.xml中有:
<many-to-one name="question" class="org.lxh.myzngt.vo.Question" fetch="select">
<column name="qid" />
</many-to-one>
所以SQL語句如下:
public List queryByUserAnswer(String userid, int currentPage, int lineSize) {
List all = null;
String hql = "from Question as q where q.qid in(select a.question.qid from Answer as a where a.userid=?)";
Query q = super.getSession().createQuery(hql);
q.setString(0, userid);
// 分頁操作。
q.setFirstResult((currentPage - 1) * lineSize);
q.setMaxResults(lineSize); all = q.list();
return all;
}
否則報錯:
org.hibernate.QueryException: could not resolve property: qid of: org.lxh.myzngt.vo.Answer [select count(q.qid) from org.lxh.myzngt.vo.Question as q where q.qid in(select a.qid from org.lxh.myzngt.vo.
Answer as a where a.userid=?)]
posted @ 2009-02-23 15:58 斷點 閱讀(173) | 評論 (0)