断点

每天进步一点点!
posts - 174, comments - 56, trackbacks - 0, articles - 21

表外键在SQL语句中的用法

Posted on 2010-01-13 23:26 断点 阅读(395) 评论(0)  编辑  收藏 所属分类: Hibernate
有两张表(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 @ 2009-02-23 15:58 断点 阅读(173) | 评论 (0)


只有注册用户登录后才能发表评论。


网站导航: