有两张表(question、answer),它们存在着一对多关系(question->answer)和多对一关系(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 on 2009-02-23 15:58  断点  阅读(424)  评论(0编辑  收藏  举报