Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Hibernate <subselect>:

Tags:

hibernate

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.

like image 518
Mohan Avatar asked May 12 '11 09:05

Mohan


People also ask

How do I create a subquery in Hibernate?

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.

Can we use select * in HQL?

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 .

What is FetchMode Subselect?

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 ...

How does Hibernate HQL work?

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.


1 Answers

  1. Based on the description given in section 5.1.3, the subselect element is used to define a read-only/immutable entity which is based on the results of an arbitrary native query.
  2. 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>
    
  3. 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.

like image 105
Ryan Ransford Avatar answered Oct 23 '22 04:10

Ryan Ransford