如何使用Hibernate框架在单个表上实现左连接?

时间:2013-12-19 04:47:37

标签: java hibernate struts hql hibernate-mapping

如何在hibernate框架中实现关联映射和左连接。请帮助我.....

这是我的hibernate映射类:

<hibernate-mapping>
<class name="com.myapp.struts.customer_tree_dao" table="gps_customer_tree">
  <id name="id" column="id">
      <generator class="increment"/>
  </id>
  <property name="customerAccount" column="customer_account"></property>
  <property name="customerName" column="customer_name"></property>
  <property name="parentId" column="parent_id"></property>
  <one-to-many class="com.myapp.struts.customer_tree_dao"></one-to-many>
  </class>
  </hibernate-mapping>

这是我的bean类:

public class customer_tree_dao {

private int id;
private String customerAccount;
private String customerName;
private int parentId;
//Getter and Setter mthods
}

这是我的动作类:

Query query_login_account = session.createQuery("from customer_tree_dao c1 left join customer_tree_dao c2 WHERE c2.customerAccount=?");
            query_login_account.setString(0, customerName);
            List Customer_Account = query_login_account.list();
            System.out.println("Left Join Query------------------------------- : "+Customer_Account);
            for (Iterator iterator = Customer_Account.iterator(); iterator.hasNext();) {
                customer_tree_dao customer_tree_dao = (customer_tree_dao) iterator.next();
                System.out.println("Customer Login Account-------------------------- : " + customer_tree_dao.getCustomerAccount());
            }

但我得到了例外:

org.hibernate.InvalidMappingException: Could not parse mapping document from resource CustomerTree.hbm.xml

我的数据库: enter image description here

我想要这个输出:

enter image description here

如何将此sql查询转换为HQL。 请帮帮我伙计............

1 个答案:

答案 0 :(得分:0)

定义一对多时,必须在类中声明集合(List或Set)以支持映射并具有相关的实体实例。

喜欢这个

   <set name="subtrees" table="gps_customer_tree"
            inverse="true" lazy="true" fetch="select">
        <key>
            <column name="parent_id" not-null="true" />
        </key>
        <one-to-many class="com.myapp.struts.customer_tree_dao" />
    </set>

在您的班级中定义Set<customer_tree_dao> subtrees字段 点击此处http://viralpatel.net/blogs/hibernate-one-to-many-xml-mapping-tutorial/

BWT:使用正确的类naving约定。班级名称应从大写字母

开始