NHibernate - 按内部集合中两项的条件获取类

发布于 2024-12-24 16:10:39 字数 1447 浏览 2 评论 0原文

我有一个类(Invoice)和一个集合(InvoiceRows)。

类 -

public class Invoice
{
     public string ID {get; set;}
     public List<InvoiceRow> InvoiceRows {get; set;}
}

public class InvoiceRow
{
    public string ID { get; set;}
    public string InvoiceID { get; set;}
    public int RowNumber { get; set;}
}

映射 -

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Domain.BusinessClasses.Invoice" table="Invoices" lazy="false">
     <id name="ID">
       <column name="ID"/>
       <generator class="assigned"/>
     </id>
     <bag name="InvoiceRows" lazy="false" cascade="save-update" inverse="true" order-by="InvoiceRowNumber">
       <key column="InvoiceID"/>
       <one-to-many class="Domain.BusinessClasses.InvoiceRow" />
     </bag>
   </class>

   <class name="Domain.BusinessClasses.InvoiceRow" table="InvoiceRows" lazy="false">
     <id name="ID">
       <column name="ID"/>
       <generator class="assigned"/>
     </id>
     <property name="InvoiceID">
       <column name="InvoiceID"/>
     </property>
     <property name="RowNumber">
       <column name="RowNumber"/>
     </property>
   </class>

我想要获取具有 RowNumber = 1 RowNumber = 2 的 InvoiceRow 的所有 Invoice 对象。最好使用 ICriterion API。

I have a class (Invoice) with a collection (InvoiceRows).

Classes -

public class Invoice
{
     public string ID {get; set;}
     public List<InvoiceRow> InvoiceRows {get; set;}
}

public class InvoiceRow
{
    public string ID { get; set;}
    public string InvoiceID { get; set;}
    public int RowNumber { get; set;}
}

Mappings -

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Domain.BusinessClasses.Invoice" table="Invoices" lazy="false">
     <id name="ID">
       <column name="ID"/>
       <generator class="assigned"/>
     </id>
     <bag name="InvoiceRows" lazy="false" cascade="save-update" inverse="true" order-by="InvoiceRowNumber">
       <key column="InvoiceID"/>
       <one-to-many class="Domain.BusinessClasses.InvoiceRow" />
     </bag>
   </class>

   <class name="Domain.BusinessClasses.InvoiceRow" table="InvoiceRows" lazy="false">
     <id name="ID">
       <column name="ID"/>
       <generator class="assigned"/>
     </id>
     <property name="InvoiceID">
       <column name="InvoiceID"/>
     </property>
     <property name="RowNumber">
       <column name="RowNumber"/>
     </property>
   </class>

I want to get all the Invoice objects that have an InvoiceRow with RowNumber = 1 and RowNumber = 2. Preferably with ICriterion API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彡翼 2024-12-31 16:10:39

我设法根据 Genius 答案找到答案。

    var dCriteria1 = DetachedCriteria.For<InvoiceRow>("r")
            .SetProjection(Projections.Property("r.RowNumber"))
            .SetProjection(Projections.Property("r.InvoiceID")) // Must be last!!!!
            .Add(Restrictions.Eq("r.RowNumber", 1));

    var dCriteria2 = DetachedCriteria.For<InvoiceRow>("r")
            .SetProjection(Projections.Property("r.RowNumber"))
            .SetProjection(Projections.Property("r.InvoiceID")) // Must be last!!!!
            .Add(Restrictions.Eq("r.RowNumber", 2));


    var invoices = Session.CreateCriteria<Invoice>()
            .Add(Subqueries.PropertyIn("ID", dCriteria1))
            .Add(Subqueries.PropertyIn("ID", dCriteria2))          
            .List<Invoice>();

I managed to find the answer based on Genius answer.

    var dCriteria1 = DetachedCriteria.For<InvoiceRow>("r")
            .SetProjection(Projections.Property("r.RowNumber"))
            .SetProjection(Projections.Property("r.InvoiceID")) // Must be last!!!!
            .Add(Restrictions.Eq("r.RowNumber", 1));

    var dCriteria2 = DetachedCriteria.For<InvoiceRow>("r")
            .SetProjection(Projections.Property("r.RowNumber"))
            .SetProjection(Projections.Property("r.InvoiceID")) // Must be last!!!!
            .Add(Restrictions.Eq("r.RowNumber", 2));


    var invoices = Session.CreateCriteria<Invoice>()
            .Add(Subqueries.PropertyIn("ID", dCriteria1))
            .Add(Subqueries.PropertyIn("ID", dCriteria2))          
            .List<Invoice>();
扛起拖把扫天下 2024-12-31 16:10:39

应该看起来像这样(未检查!):

var dCriteria1 = DetachedCriteria.For<InvoiceRow>("r")
        .Add(Restrictions.EqProperty("r.InvoiceID", "i.ID"))
        .Add(Restrictions.Eq("r.RowNumber", 1));
var dCriteria2 = DetachedCriteria.For<InvoiceRow>("r")
        .Add(Restrictions.EqProperty("r.InvoiceID", "i.ID"))
        .Add(Restrictions.Eq("r.RowNumber", 2));

var invoices = Session.CreateCriteria<Invoice>("i")
   .Add(Subqueries.Exists(dCriteria1))
   .Add(Subqueries.Exists(dCriteria2))
   .List<Invoice>();

也许这段代码可以优化,但想法就是这样。

should look like this (not checked!):

var dCriteria1 = DetachedCriteria.For<InvoiceRow>("r")
        .Add(Restrictions.EqProperty("r.InvoiceID", "i.ID"))
        .Add(Restrictions.Eq("r.RowNumber", 1));
var dCriteria2 = DetachedCriteria.For<InvoiceRow>("r")
        .Add(Restrictions.EqProperty("r.InvoiceID", "i.ID"))
        .Add(Restrictions.Eq("r.RowNumber", 2));

var invoices = Session.CreateCriteria<Invoice>("i")
   .Add(Subqueries.Exists(dCriteria1))
   .Add(Subqueries.Exists(dCriteria2))
   .List<Invoice>();

may be this code can be optimized, but idea is such.

小…楫夜泊 2024-12-31 16:10:39
List<int> list=new List<int>();
list.Add(1);
list.Add(2);
DetachedCriteria forInvoice=DetachedCriteria.For<Invoice>();
DetachedCriteria forInvoiceRow=forInvoice.CreateCriteria("InvoiceRows");
forInvoiceRow.Add(Expression.In("RowNumber",list));
ICriteria executableCriteria = forInvoice.GetExecutableCriteria(Session);
executableCriteria.List<Invoice>();
List<int> list=new List<int>();
list.Add(1);
list.Add(2);
DetachedCriteria forInvoice=DetachedCriteria.For<Invoice>();
DetachedCriteria forInvoiceRow=forInvoice.CreateCriteria("InvoiceRows");
forInvoiceRow.Add(Expression.In("RowNumber",list));
ICriteria executableCriteria = forInvoice.GetExecutableCriteria(Session);
executableCriteria.List<Invoice>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文