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.

2 Answers 2

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.

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);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.