gpt4 book ai didi

c# - 从不同表中选择的休眠标准

转载 作者:行者123 更新时间:2023-12-03 19:51:17 25 4
gpt4 key购买 nike

我有以下表格模型:



我想要以下SQL命令作为休眠标准:


SELECT * FROM OHER IN所在的单位(SELECT OID FROM Orders在何处
PONumber <> 0按PONumber限制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>


编辑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);


谢谢

最佳答案

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

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

关于c# - 从不同表中选择的休眠标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16338535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com