Hibernate fetch=join/select 不适用于 HQL?

Hibernate fetch=join/select is not applicable for HQL?

我已经使用 fetch=join.And 编写了 HQL 查询,它是在 set 中配置的。如果我尝试使用父休眠检索数据,则不会生成连接语句。它正在使用 N+1 获取数据。为什么 hibernate 不能为父记录和子记录生成单个连接语句。

映射文件:

<hibernate-mapping>
    <class name="com.joins.test.Vendor" table="vendor">
        <id name="vendorid" column="vid">
            <generator class="native" />
        </id>
        <property name="vendorname" type="string">
            <column name="vname" length="10" not-null="true" />
        </property>
        <set name="children" inverse="false" cascade="all" lazy="false" fetch="join">
            <key column="vendid" not-null="true" />
            <one-to-many class="com.joins.test.Customer" />
        </set>
    </class>
</hibernate-mapping>


<hibernate-mapping>
    <class name="com.joins.test.Customer" table="customer">
        <id name="customerid" column="cid">
            <generator class="native" />
        </id>
        <property name="customername" type="string">
            <column name="cname" length="10" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

这是我获取数据的代码:

Query q=sn.createQuery("from Vendor");
        List<Vendor> l=q.list();
        for(Vendor v:l)
        {
            System.out.print(" vendorname "+v.getVendorname());
            Set<Customer> s=v.getChildren();
            Iterator<Customer> it=s.iterator();
            while(it.hasNext()){
                Customer cs=it.next();
                System.out.println(" customername "+cs.getCustomername());
            }
        }

HQL 的工作方式与 sql 相似,唯一不同的是 HQL 在我们触发任何查询时支持休眠实体。

在您的情况下,如果您想要 "join" 查询,则必须明确使用 "joins",因为 HQL 避免使用 fetch="join"fetch="select".