用于从不同表中进行选择的nhibernate标准

时间:2013-05-02 12:51:36

标签: c# sql sqlite nhibernate

我有以下表格模型:

enter image description here

我希望将以下SQL命令作为nhibernate条件:

  

SELECT * FROM Units WHERE OID IN(从订单WHERE中选择OID   PONumber<> 0订购PONumber LIMIT 5)

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

编辑:

我的映射

Unit.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>

Order.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>

Employee.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>

EDIT2:

这是我的查询命令:

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

THX

1 个答案:

答案 0 :(得分: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);