如何在Hibernate *.hbm.xml 文件中定义关联查询

解决方案 »

  1.   

    可以这样的,举例说明 吧,问答功能:有问题类别和问题,一个类别对对应多个问题,那么在ASk.hbm.xml里,添加这个:
    <property name="replycount" type="integer" update="true"
    insert="true" column="replycount" not-null="true">
    </property>
    <many-to-one name="qtype" class="com.clove.cms.model.QType"
    insert="false" update="false" column="tid" />
    在ASK 这个Entity中,添加
    /问题类别
    private QType qtype = new QType();

    //问题类别名称

    private String typeName=props.getDEFAULT_EMPTY();public QType getQtype() {
    return qtype;
    } public void setQtype(QType qtype) {
    setTypeName(qtype);
    this.qtype = qtype;
    } public String getTypeName() {
    return typeName;
    } public void setTypeName(QType type) {
    try {
    if(type != null && type.getTitle()!=null){
    this.typeName=type.getTitle();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }这样,就可以进行表的关连了。
      

  2.   

    在进行复和查询时,可以用HQL或Criteria操作, 建议用后者,完全面向对象向的查询语句,很方便的。