Hi All,
Most of our people using list in hibernate query for list of maps and iterating and converting in to list of maps. Instead of that we can reduce the execution cycle and we can improve the performance as well. A special thanks to Mr. Loganathan who is my team met to sharing this article with me.
select c.name, c.orderfrom customer c
In my opinion iterating over lists of Object[] is
verbose and error-prone because processing the result-list is directly
dependant on the sequence of selected properties in the hql string. If
you change the query string you have to change the sourcecode as well.
Check out the following snippet (HQL specific feature):
select new map(c.name as name, c.order as order)
from Customer c
Instead
of returning arrays this query will return a list of maps each
containing entries with aliases (the keys) to selected values.
Example in our project:
List<Map<String, Object>> itemList = getHibernateTemplate().find("select new map(item.id as id,item.aditmName as name,item.molecule as molecule) from Item as item");
Instead
of returning arrays this query will return a list of maps each
containing entries with aliases (the keys) to selected values:
[{molecule=ANTIOXIDANTS;MULTIVITAMIN;MULTIMINERAL;METHYCOBALAMINE;, id=ITEM-744786, name=A TO ZTAB}, {molecule=OMEGA3FATTYACIDS;GREENTREEEXTRACT;GINKGOBILOBA;GINSENG;GRAPESEEDEXTRACT;ANTIOXIDANTS;VITAMINS;MINERALS;TRACEELEMENTS;, id=ITEM-744794, name=ABSOLUT 3G CAP}, {molecule=ANTIOXIDANTS;MULTIVITAMIN;MULTIMINERAL;;, id=ITEM-745166, name=CARDIOPLUS CAP}]
JavaScript using jQuery Template:
Example in our project:
$.template("itemListTemplate","<a><dt>${name}</dt><dd><span class='item_id'>${id}</span><p>${molecule}</p></dd></a>");
$.tmpl( "itemListTemplate", response.itemList ).appendTo( "#alternativeMedicineListId" );
this generate the html list:
<dl id="alternativeMedicineListId">
<a>
<dt>A TO ZTAB</dt>
<dd><span class="item_id">ITEM-744786</span><p>ANTIOXIDANTS;MULTIVITAMIN;MULTIMINERAL;METHYCOBALAMINE;</p></dd>
</a>
<a class="">
<dt>ABSOLUT 3G CAP</dt>
<dd><span class="item_id">ITEM-744794</span><p>OMEGA3FATTYACIDS;GREENTREEEXTRACT;GINKGOBILOBA;GINSENG;GRAPESEEDEXTRACT;ANTIOXIDANTS;VITAMINS;MINERALS;TRACEELEMENTS;</p></dd>
</a>
<a class="">
<dt>CARDIOPLUS CAP</dt>
<dd style="border: medium none;"><span class="item_id">ITEM-745166</span><p>ANTIOXIDANTS;MULTIVITAMIN;MULTIMINERAL;;</p></dd>
</a>
</dl>
// here response.itemList is JSON Object
// "alternativeMedicineListId" is id of the list(ex:ul,dl) in jsp
// here i am using definition list
<dl id="alternativeMedicineListId"></dl>