【问题标题】:How to use Hibernate <subselect>:如何使用 Hibernate <subselect>:
【发布时间】:2011-05-12 09:10:03
【问题描述】:

我是休眠新手。我需要了解以下问题:

(1)什么是hibernate映射中的子选择?

(2)如何在hbm文件中映射subselect?

(3) 如果我使用 subselect 检索值,那么如何在 java Action 类中获取检索到的值。

【问题讨论】:

    标签: hibernate


    【解决方案1】:
    1. 根据section 5.1.3 中给出的描述,subselect 元素用于定义基于任意本机查询结果的只读/不可变实体。
    2. 从同一来源,人们只需在class 元素中使用subselect 而不是table 属性,然后将查询中定义的列名用作属性映射中的列名。 (以下内容逐字逐句摘自第 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. 使用 subselect 元素中的查询列创建映射后,您应该能够像访问任何其他实体一样访问属性。

    【讨论】:

      【解决方案2】:

      实际上您不需要为子选择建模,您可以创建使用它们的查询。检查:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-subqueries

      (编辑:上面链接中的示例)

      String hql = "from Cat as fatcat "+
                   "where fatcat.weight > ( "+
                   "  select avg(cat.weight) from DomesticCat cat "+
                   ")";
      List fatcats = session.createQuery(hql);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-05
        • 2017-03-09
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 2012-04-25
        • 1970-01-01
        相关资源
        最近更新 更多