与过滤视图的多对多关系

时间:2013-11-25 16:29:31

标签: hibernate

我的工厂与公司的关系是多对多的,因为工厂可以为多家公司生产,公司可以拥有多家工厂。对于我正在开发的应用程序,我们创建了一个视图,根据特定条件过滤公司列表。

当我去填充我的工厂列表时,我收到一个错误,因为它查找了连接表中的公司但在过滤后的公司视图中没有记录。有没有办法设置它,以便不返回没有公司的工厂或我需要重新设计我的视图设置。

示例:

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>

1 个答案:

答案 0 :(得分: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>
相关问题