My Problem/Questions are below...
Mapping documents (Parent):
<hibernate-mapping>
<class name="com.afm.pojo.AfmArtItem" table="AFM_ITEM">
<id name="artId" column="ARTID">
<generator class="identity" />
</id>
<many-to-one name="seller"
column="UID" not-null="true"
class="com.afm.pojo.AfmSeller"/>
<property name="description" type="string" column="DESCRIPTION"/>
<property name="title" type="string"/>
<property name="size" type="string"/>
<property name="price" type="big_decimal"/>
<property name="shippingCost" type="big_decimal"/>
<property name="isAvailable" type="boolean"/>
<!--- LOOK THIS --->
<set name="categories" table="AFM_ITEM_CATEGORY" cascade="all" inverse="true" lazy="false">
<key column="artId"/>
<one-to-many class="com.afm.pojo.AfmArtItemCategory"/>
</set>
</class>
<!-- AND ALSO LOOK HERE -->
<query name="findAllArtItemsByCategory">
<![CDATA[
select item
from com.afm.pojo.AfmArtItem as item inner join item.categories as categories
where categories.category =?
]]>
</query>
</hibernate-mapping>
Mapping documents (Child):
<hibernate-mapping>
<class name="com.afm.pojo.AfmArtItemCategory" table="AFM_ITEM_CATEGORY">
<id name="categoryId" column="CATEGORYID">
<generator class="identity" />
</id>
<!-- LOOK HERE -->
<many-to-one name="artItem"
column="artId" not-null="true"
class="com.afm.pojo.AfmArtItem"/>
<property name="category" type="string" column="CATEGORY"/>
</class>
</hibernate-mapping>
Code:
@SuppressWarnings("unchecked")
public List<AfmArtItem> findAllArtItemsByCategory(String category) {
return getHibernateTemplate().findByNamedQuery("findAllArtItemsByCategory", category);
}
When code is run, it returns 10 records but it generates 11 select statement. One is for joining Like this
select afmartitem0_.ARTID as ARTID3_, afmartitem0_.UID as UID3_, afmartitem0_.DESCRIPTION as DESCRIPT3_3_, afmartitem0_.title as title3_, afmartitem0_.size as size3_, afmartitem0_.price as price3_, afmartitem0_.shippingCost as shipping7_3_, afmartitem0_.isAvailable as isAvaila8_3_ from AFM_ITEM afmartitem0_ inner join AFM_ITEM_CATEGORY categories1_ on afmartitem0_.ARTID=categories1_.artId where categories1_.CATEGORY=?
and another 10 select statements from child table. Like below.
Does anyone know why this is happening? This behavior is not acceptable since too many SQL executed for no reason so it downgrade performance.
I think I have done something wrong, maybe wrong mapping which I don't know. Please help. quick solution to fix this problem or better solution to do simply better way.
This is a simple relationship "Item as parent" and "Category as child". Item will have many categories and I should be able to search by Category.
Thanks.
select categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
elect categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
select categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
select categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
select categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
select categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
select categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
select categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
select categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
select categories0_.artId as artId1_, categories0_.CATEGORYID as CATEGORYID1_, categories0_.CATEGORYID as CATEGORYID4_0_, categories0_.artId as artId4_0_, categories0_.CATEGORY as CATEGORY4_0_ from AFM_ITEM_CATEGORY categories0_ where categories0_.artId=?
[/b][/b]