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?
(3) If i retrieve values using subselect then how to get the retrieved values in java Action class.
For databases that support subselects, Hibernate supports subqueries within queries. A subquery must be surrounded by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed.
HQL From: HQL From is same as select clause in SQL, from Employee is same as select * from Employee . We can also create alias such as from Employee emp or from Employee as emp .
The FetchMode.SUBSELECT. use a subselect query to load the additional collections. Hibernate docs: If one lazy collection or single-valued proxy has to be fetched, Hibernate will load all of them, re-running the original query in a subselect. This works in the same way as batch-fetching but without the piecemeal ...
Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With