我正在尝试实现HQL查询。我已经能够在SQL中实现它 - 我对它更熟悉一点。我一直挂着的是INNER JOINS。
这些类是这样实现的......
class Item
class Component extends Item
private Item parentItem;
class Assembly extends Item
到目前为止,这就是我对HQL的看法......
SELECT
item.blah,
comp.blah,
assembly.blah
FROM
Component comp
LEFT OUTER JOIN comp.parentItem item,
Assembly assembly
WHERE
item.parentItem = assembly
这是有效的 - 除了我需要最后三行是LEFT OUTER JOIN而不是互斥条件。我试图以多种方式实现这一点 - 但我一直在遇到映射问题。
<hibernate-mapping>
<class lazy="false" name="com.kcp.common.domain.inventory.Item"
table="W_INV_INV_ITEM" where="deleted=0">
<joined-subclass lazy="false" name="com.kcp.common.domain.inventory.Component" table="W_INV_INV_COMPONENT">
<key>
<column name="ID">
<comment>Primary and foreign key to W_INV_INV_ITEM.</comment>
</column>
</key>
<many-to-one cascade="all" class="com.kcp.common.domain.inventory.Item" name="parentItem" outer-join="true">
<column name="PARENT_ITEM_ID">
<comment>Foreign key identifying the item to which this component is assembled.</comment>
</column>
</many-to-one>
</joined-subclass>
<joined-subclass lazy="false" name="com.kcp.common.domain.inventory.Assembly" table="W_INV_INV_MAJOR_ASSEMBLY">
<key>
<column name="ID">
<comment>Primary and foreign key to W_INV_INV_ITEM.</comment>
</column>
</key>
</class>
</hibernate-mapping>
另外 - 我让它像SQL一样工作......
FROM
DBO.W_INV_INV_ITEM item
INNER JOIN DBO.W_INV_INV_COMPONENT comp ON item.id = comp.id
LEFT OUTER JOIN DBO.W_INV_INV_ITEM parentInv ON comp.PARENT_ITEM_ID = parentInv.id
LEFT OUTER JOIN DBO.W_INV_INV_MAJOR_ASSEMBLY parentMA ON comp.PARENT_ITEM_ID = parentMA.id
答案 0 :(得分:3)
如果在WHERE子句中包含LEFT JOIN条件,它将充当INNER JOIN。
如果item
是可选的,那么item.parentItem
也必须是可选的,因此您需要将其包含在LEFT JOIN中。
该HQL查询将生成可怕的SQL查询,您仍应优化它。
尝试这样的事情:
SELECT
i.blah,
c.blah
FROM Component c
LEFT JOIN c.parentItem i
LEFT JOIN i.parentItem p
WHERE
p is null or p.class = 'Assembly'
class Item
class Component extends Item
private Item parentItem;
class Assembly extends Item