I have written the HQL query using fetch=join.And it is configured in set. If I tried to retrieve the data using parent hibernate is not generating the join statement. It is getting the data using N+1. Why hibernate is not able to generate the single join statement for both parent and child records.
Mapping files:
<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>
This is my code for fetching data:
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 works like sql only the difference is that HQL support hibernate Entity while we fire any query.
In your case if you want "join" query you will have to use "joins" explicitly because HQL avoids fetch="join"
or fetch="select"
.