How to use Hibernate :

后端 未结 2 947
南笙
南笙 2020-12-06 13:40

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?

相关标签:
2条回答
  • 2020-12-06 14:05

    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 讨论(0)
  • 2020-12-06 14:16
    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.

    0 讨论(0)
提交回复
热议问题