NHibernate:使用 OR 运算符查询

发布于 2024-09-19 11:42:44 字数 2071 浏览 9 评论 0原文

我正在寻找一种在查询中构建 OR 运算符的方法,以在表的一个字段以及连接表的另一个字段中查找特定值。这在 SQL 中是非常基本的,但我不知道如何在 NHibernate 中做到这一点。我一直在网上搜索,但我找到的示例对我来说非常模糊,我发现它们很难应用于我的特定实现。

我有一个名为 Party 的类,其中有一个名为引用的字符串字段,这是主要引用。新的要求要求能够选择向一方添加大量侧面引用。因此,我必须添加另一个名为 PartyReference 的类,它与 Party 具有多对一的关系。

现在有了给定的参考,我必须在这个主要参考字段以及辅助参考中查找它的值。但只要我无法弄清楚如何对 NHibernate 说该字段必须对应于该值或其他字段之一,我就无法使其工作。

我已经做了一个看起来像这样的解决方法,但它既不优雅又愚蠢,因为必须有办法说“OR”:

   public Party GetPartyOnAnyReference(string reference)
       {
           Party party;

           ISession session = Factory.OpenSession();
           ITransaction tx = session.BeginTransaction();
           try
           {
               //first search on main reference
               ICriteria criteria1 = session.CreateCriteria(typeof(Party));
               criteria1.Add(Restrictions.Eq("Reference", reference));
               IList<Party> parties1 = criteria1.List<Party>();
               party = parties1.Count > 0 ? parties1[0] : null;

               //then try with side-references
               if (party == null)
               {
                   ICriteria criteria2 = session.CreateCriteria(typeof(Party));
                   criteria2
                           .SetFetchMode("References", FetchMode.Eager)
                           .CreateCriteria("References")
                           .Add(Expression.Eq("Reference", reference));
                   IList<Party> parties2 = criteria2.List<Party>();
                   party = parties2.Count > 0 ? parties2[0] : null;
               }

               session.Close();
           }
           catch (Exception e)
           {
               tx.Rollback();
               session.Close();

               if (e.GetType().ToString() == "NHibernate.ObjectNotFoundException")
                   party = null;
               else throw;
           }
           return party;
       }

我当然意识到我也可以通过简单地从 Party 类中删除主要引用来解决这个问题并将其与其他引用同等对待,作为 PartyReference。但在某个阶段,无论如何我都必须使用 NHibernate 的 OR 查询,所以我现在最好用这种特殊情况来解决它。

有什么想法吗?

I am looking for a way to build an OR operator into a query to look for a particular value in one field of a table as well as in another field of a joined table. This is pretty basic in SQL, but I can't for the world figure out how to do this in NHibernate. I have been searching the web, but the examples I find are pretty nebulous to me and I find them hard to apply to my particular implementation.

I have an class called Party, with a string-field called reference, which is the main reference. New requirements demanded the option to also being able to add a lot of side-references to a party. So I had to add another class called PartyReference that has a many-to-one relationship to Party.

Now with a given reference I have to look its value up in both this main reference field as well as among the side references. But as long as I cannot figure out to say to NHibernate that EITHER this field must correspond to the value OR one of the others, I cannot make it work.

I have made a workaround that looks like this, but it is inelegant and stupid, as there has to be way to say "OR":

   public Party GetPartyOnAnyReference(string reference)
       {
           Party party;

           ISession session = Factory.OpenSession();
           ITransaction tx = session.BeginTransaction();
           try
           {
               //first search on main reference
               ICriteria criteria1 = session.CreateCriteria(typeof(Party));
               criteria1.Add(Restrictions.Eq("Reference", reference));
               IList<Party> parties1 = criteria1.List<Party>();
               party = parties1.Count > 0 ? parties1[0] : null;

               //then try with side-references
               if (party == null)
               {
                   ICriteria criteria2 = session.CreateCriteria(typeof(Party));
                   criteria2
                           .SetFetchMode("References", FetchMode.Eager)
                           .CreateCriteria("References")
                           .Add(Expression.Eq("Reference", reference));
                   IList<Party> parties2 = criteria2.List<Party>();
                   party = parties2.Count > 0 ? parties2[0] : null;
               }

               session.Close();
           }
           catch (Exception e)
           {
               tx.Rollback();
               session.Close();

               if (e.GetType().ToString() == "NHibernate.ObjectNotFoundException")
                   party = null;
               else throw;
           }
           return party;
       }

I of course realize I can also solve this issue by simply removing the main reference from the Party class alltogether and treat it on par with the other references, as a PartyReference. But at some stage I will have to use an OR query with NHibernate anyway, so I might just as well solve it now with this particular case.

Any ideas?

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

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

发布评论

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

评论(1

和我恋爱吧 2024-09-26 11:42:44

您可以使用 Restrictions.Or 或对多个 or 使用 Disjunction。

session.CreateCriteria<Party>()
    .CreateAlias("References", "r", JoinType.LeftOuterJoin)
    .Add(Restrictions.Or(
        Restrictions.Eq("Reference", reference),
        Restrictions.Eq("r.Reference", reference)))
    .SetResultTransformer(new DistinctRootEntityResultTransformer())
    .List<Party>();

You can use Restrictions.Or or use a Disjunction for multiple or's.

session.CreateCriteria<Party>()
    .CreateAlias("References", "r", JoinType.LeftOuterJoin)
    .Add(Restrictions.Or(
        Restrictions.Eq("Reference", reference),
        Restrictions.Eq("r.Reference", reference)))
    .SetResultTransformer(new DistinctRootEntityResultTransformer())
    .List<Party>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文