使用自连接和 NHibernate Criteria API 查询覆盖实体

发布于 2024-07-22 07:23:47 字数 1191 浏览 7 评论 0原文

我有一个简单的豁免模型,我想进行一个查询,返回所有未被覆盖的豁免。

public class Waiver
{
    private readonly int id;

    protected Waiver()
    {
        this.id = 0;
    }

    public virtual int Id { get { return id; } }

    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual bool IsRequired { get; set; }
    public virtual DateTime EffectiveDate { get; set; }

    public virtual Waiver OverriddenWaiver { get; set; }
}

地图如下:

  <class name="Waiver" table="Music_Waivers">
    <id name="id" access="field" column="WaiverId" unsaved-value="0">
      <generator class="native" />
    </id>

    <property name="Name" column="Name" />
    <property name="Description" column="Description" />
    <property name="IsRequired" column="IsRequired" />
    <property name="EffectiveDate" column="EffectiveDate" />

    <many-to-one name="OverriddenWaiver" class="Waiver" column="OverrideWaiverId" />
  </class>

现在我希望在我的存储库中拥有一个带有签名 public IList GetLatest() 的方法。 由于某种原因,我很难使用 CriteriaAPI 来实现这一点。 我可以用 T-SQL 编写这个,没有问题。

I have a simple Waiver model, and I would like to make a query that returns all the Waivers that are not overridden.

public class Waiver
{
    private readonly int id;

    protected Waiver()
    {
        this.id = 0;
    }

    public virtual int Id { get { return id; } }

    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual bool IsRequired { get; set; }
    public virtual DateTime EffectiveDate { get; set; }

    public virtual Waiver OverriddenWaiver { get; set; }
}

Here's the map:

  <class name="Waiver" table="Music_Waivers">
    <id name="id" access="field" column="WaiverId" unsaved-value="0">
      <generator class="native" />
    </id>

    <property name="Name" column="Name" />
    <property name="Description" column="Description" />
    <property name="IsRequired" column="IsRequired" />
    <property name="EffectiveDate" column="EffectiveDate" />

    <many-to-one name="OverriddenWaiver" class="Waiver" column="OverrideWaiverId" />
  </class>

Now I want to have a method in my Repository with the signature public IList GetLatest(). For some reason I'm having a hard time implementing this with the CriteriaAPI. I can write this in T-SQL no problem.

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

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

发布评论

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

评论(1

烟火散人牵绊 2024-07-29 07:23:47

我最终暴力破解了一个解决方案。 它不太漂亮,但因为我知道表会很小(可能最终只有 5 行),所以我想出了以下代码解决方案:

public IList<Waiver> GetLatest()
{
    using (var session = SessionManager.OpenSession())
    {               
        var criteria = session.CreateCriteria(typeof (Waiver));
        var waivers = criteria.List<Waiver>();

        var nonOverridenWaivers = new List<Waiver>();

        foreach(var waiver in waivers)
        {
            bool overrideExists = waivers.Any(w => w.Overrides != null &&
                                                   w.Overrides.Id == waiver.Id);
            if (!overrideExists)
                nonOverridenWaivers.Add(waiver);
        }

        return nonOverridenWaivers;
    }
}

I ended up brute forcing a solution. It's not pretty, but since I know the table will be tiny (probably going to end up being only 5 rows) I came up with the following code solution:

public IList<Waiver> GetLatest()
{
    using (var session = SessionManager.OpenSession())
    {               
        var criteria = session.CreateCriteria(typeof (Waiver));
        var waivers = criteria.List<Waiver>();

        var nonOverridenWaivers = new List<Waiver>();

        foreach(var waiver in waivers)
        {
            bool overrideExists = waivers.Any(w => w.Overrides != null &&
                                                   w.Overrides.Id == waiver.Id);
            if (!overrideExists)
                nonOverridenWaivers.Add(waiver);
        }

        return nonOverridenWaivers;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文