簡體   English   中英

如何使用Hibernate框架在單個表上實現左聯接?

[英]How to implement left join on single table using Hibernate framework?

我該如何在Hibernate Framework中實現關聯映射和左連接。請幫助我.....

這是我的休眠映射類:

<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

我的數據庫: 在此處輸入圖片說明

我想要這個輸出:

在此處輸入圖片說明

如何將這個SQL查詢轉換為HQL。 請幫我.........

當定義一對多時,必須在類中聲明集合(列表或集合)以支持映射並具有相關的實體實例。

像這樣

   <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:使用適當的類慣例。 班級名稱應以大寫字母開頭

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM