i am new to hibernate. i need to understand the following questions :
(1) What is subselect in hibernate mapping?
(2) How to map subselect in hbm file?
Actually you don't need to model subselects, you can create queries that uses them. Check: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-subqueries
(Edit: example from the link above)
String hql = "from Cat as fatcat "+
"where fatcat.weight > ( "+
" select avg(cat.weight) from DomesticCat cat "+
")";
List fatcats = session.createQuery(hql);
subselect
element is used to define a read-only/immutable entity which is based on the results of an arbitrary native query.From the same source, one simply uses subselect
within a class
element instead of the table
attribute and then uses the column names defined in the query as column names in the property mapping. (the following is taken verbatim from section 5.1.3)
<class name="Summary">
<subselect>
select item.name, max(bid.amount), count(*)
from item
join bid on bid.item_id = item.id
group by item.name
</subselect>
<synchronize table="item"/>
<synchronize table="bid"/>
<id name="name"/>
...
</class>
After you create a mapping using columns from the query in the subselect
element, you should be able to access the properties just as you would for any other entity.