2013-11-25 55 views
0

我擁有多對多的工廠對企業關係,因爲工廠可以爲多家公司生產,而且公司可以擁有多家工廠。對於我正在開發的應用程序,我們創建了一個視圖來按照特定條件過濾公司列表。與過濾視圖的多對多關係

當我去填充我的工廠列表時,我收到一個錯誤,因爲它查找處於其連接表中的公司但在篩選後的公司視圖中沒有記錄。有沒有辦法設置這樣的設置,使沒有公司的工廠不會退回,或者我需要重新修改我的視圖設置。

例子:

Plant Table 
Plant A 
Plant B 
Plant C 

Filtered Company Table View 
Company 1 
Company 2 
Company 3 

Join Table 
Plant A -> Company 1 
Plant A -> Company 2 
Plant B -> Company 4 
Plant C -> Company 3 
Plant C -> Company 4 

Output -> error, company 4 does not exist 
Desired Output -> Plant A(Company 1,2) - Plant C(Company 3) 

我在Plant.hbm.xml文件條目。

<set name="company" table="plant_company_join_table" schema="myschema" lazy="false"> 
      <key> 
       <column name="plant_id" not-null="true"/> 
    </key> 
    <many-to-many class="com.redacted.Company"> 
     <column name="company_id" not-null="true"/> 
    </many-to-many> 
</set> 

回答

0

因此,當我放棄並詢問互聯網時,我當然會找到答案。所以對於那些將來有這個問題的人,這就是我所做的。從'set'標籤中刪除表格和模式,然後添加一個'subselect'標籤,該標籤用公司表格中的條目過濾連接表格。希望這可以幫助!!!

<set name="company" lazy="false"> 
    <subselect> 
     select plant_id, company_id from myschema.plant_company_join_table jntbl 
     join myschema.company_view using (plant_company_id) 
    </subselect> 
    <key> 
     <column name="plant_id" not-null="true"/> 
    </key> 
    <many-to-many class="com.redacted.Company"> 
     <column name="company_id" not-null="true"/> 
    </many-to-many> 
</set>