Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将内部连接和左连接与JPQL和HQL结合起来_Java_Sql_Hibernate_Hql_Hibernate Mapping - Fatal编程技术网

Java 如何将内部连接和左连接与JPQL和HQL结合起来

Java 如何将内部连接和左连接与JPQL和HQL结合起来,java,sql,hibernate,hql,hibernate-mapping,Java,Sql,Hibernate,Hql,Hibernate Mapping,我正在尝试实现一个HQL查询。我已经能够在SQL中实现它——我对SQL更为熟悉。我一直担心的是内部连接 这些类是这样实现的 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

我正在尝试实现一个HQL查询。我已经能够在SQL中实现它——我对SQL更为熟悉。我一直担心的是内部连接

这些类是这样实现的

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
这是可行的-除了我需要最后三行是左外连接,而不是相互排斥的条件。我尝试了很多方法来实现这一点,但我一直遇到映射问题

<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>

如果在WHERE子句中包含左连接条件,它将充当内部连接

如果项是可选的,那么item.parentItem也必须是可选的,因此需要将其包含在左联接中

试着这样做:

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'
 

如果在WHERE子句中包含左连接条件,它将充当内部连接

如果项是可选的,那么item.parentItem也必须是可选的,因此需要将其包含在左联接中

试着这样做:

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'
 

越来越近-只要我添加行LEFT JOIN I.parentItem p,我仍然会收到一个SQL.GrammerException错误。我知道它将parentItem识别为成员变量,但它仍然抛出grameRexception错误。您需要发布实体。我添加了hibernate映射的摘要版本。这行吗?越来越近了-我还是会在添加左JOIN I.parentItem p行时出现SQL.grameRexception错误。我知道它将parentItem识别为成员变量,但它仍然抛出grameRexception错误。您需要发布实体。我添加了hibernate映射的摘要版本。这样行吗?