How to use Hibernate <subselect>:
Asked Answered
B

2

7

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.

Baize answered 12/5, 2011 at 9:10 Comment(0)
R
7
  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.

Richter answered 24/8, 2011 at 14:44 Comment(0)
T
2

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);
Triturate answered 12/5, 2011 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.