1

我有以下表格模型:

在此处输入图像描述

我希望以下 SQL 命令作为休眠条件:

SELECT * FROM Units WHERE OID IN (SELECT OID FROM Orders WHERE PONumber <> 0 ORDER BY PONumber LIMIT 5)

-> 换句话说:最后 5 个订单

编辑:

我的映射

单位.hbm.xml

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="DCGOrderSystem"
                  namespace="DCGOrderSystem.Model">

  <class name="Unit" table="Units">
    <id name="ID">
      <column name="UID" />
      <generator class="native" />
    </id>

    <property name="Division" />
    <property name="Date" />
    <property name="ItemOrderNr" />
    <property name="Description" />
    <property name="Amount" />
    <property name="Price" />
    <property name="CostCenter" />
    <property name="Location" />
    <property name="DeviceGroup" />
    <property name="Comment" />
    <property name="Distributor" />

    <!-- Many-to-one mapping: Employee -->
    <many-to-one name="EmployeeRef" column="EID" cascade="all" />

    <!-- Many-to-one mapping: Order -->
    <many-to-one name="OrderRef" column="OID" cascade="all" />
  </class>

</hibernate-mapping>

订单.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="DCGOrderSystem"
                  namespace="DCGOrderSystem.Model">

  <class name="Order" table="Orders">
    <id name="ID">
      <column name="OID" />
      <generator class="native" />
    </id>

    <property name="Locked" />
    <property name="PONumber" />

    <!-- One-to-many mapping: Units -->
    <bag name="Units" cascade="all" lazy="true">
      <key column="OID" />
      <one-to-many class="Unit" />
    </bag>
  </class>

</hibernate-mapping>

员工.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="DCGOrderSystem"
                  namespace="DCGOrderSystem.Model">

  <class name="Employee" table="Employees">
    <id name="ID">
      <column name="EID" />
      <generator class="native" />
    </id>

    <property name="Name" />   
  </class>

</hibernate-mapping>

编辑2:

这是我的查询命令:

var query = m_hibernateSession.QueryOver<Model.Order>(() => orderAlias)
                    .JoinAlias(() => orderAlias.ID, () => unitAlias, JoinType.InnerJoin)
                    .TransformUsing(Transformers.DistinctRootEntity)
                    .OrderBy(x => x.PONumber).Desc.Take(5);

谢谢

4

1 回答 1

1

如果您想要最后五个订单,我会使用稍微不同的查询来解决问题

Select * 
From order o join Units U on O.OID = U.OID
Order by O.PONumber limit 5

在 nhibernate 中会是这样的(未测试)

Order orderAlias = null;
Unit unitAlias = null;
var query = session.QueryOver<Order>(() => orderAlias)
   .JoinAlias(() => orderAlias.Units, () => unitAlias, JoinType.InnerJoin)
   //.TransformUsing(Transformers.DistinctRootEntity) if you have duplicates
   .OrderBy(x => x.PONumber).Desc.Take(5);

更新

您也可以只加载Order实体。NHibernate 将根据您的映射信息(延迟/急切加载或连接)自动加载所有单元行。

var query = session.QueryOver<Order>().OrderBy(x => x.PONumber).Desc.Take(5);
于 2013-05-02T13:53:53.877 回答