8

我对 NHibernate 有疑问,我似乎找不到任何解决方案。在我的项目中,我有一个简单的实体(批处理),但是每当我尝试运行以下测试时,都会出现异常。我已经尝试了几种不同的方法来执行类似的查询,但几乎所有的例外都是相同的(它执行的 LINQ 方法不同)。

第一个测试:

[Test]
public void QueryLatestBatch()
{
    using (var session = SessionManager.OpenSession())
    {
        var batch = session.Query<Batch>()
            .FirstOrDefault();

        Assert.That(batch, Is.Not.Null);
    }
}

例外:

System.NullReferenceException : Object reference not set to an instance of an object.
at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, ref IQuery query, ref NhLinqExpression nhQuery)
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
at System.Linq.Queryable.FirstOrDefault(IQueryable`1 source)

第二个测试:

[Test]
public void QueryLatestBatch2()
{
    using (var session = SessionManager.OpenSession())
    {
        var batch = session.Query<Batch>()
            .OrderBy(x => x.Executed)
            .Take(1)
            .SingleOrDefault();

        Assert.That(batch, Is.Not.Null);
    }
}

例外:

System.NullReferenceException : Object reference not set to an instance of an object.
at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, ref IQuery query, ref NhLinqExpression nhQuery)
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
at System.Linq.Queryable.SingleOrDefault(IQueryable`1 source)

但是,这个正在传递(使用 QueryOver<>):

[Test]
public void QueryOverLatestBatch()
{
    using (var session = SessionManager.OpenSession())
    {
        var batch = session.QueryOver<Batch>()
            .OrderBy(x => x.Executed).Asc
            .Take(1)
            .SingleOrDefault();

        Assert.That(batch, Is.Not.Null);
        Assert.That(batch.Executed, Is.LessThan(DateTime.Now));
    }
}

使用 QueryOver<> API 一点也不差,但我有点困惑 Query<> API 不工作,这有点令人难过,因为 First() 操作非常简洁,我们的开发人员真的很喜欢 LINQ。

我真的希望有一个解决方案,因为如果这些方法未能通过如此简单的测试似乎很奇怪。

编辑

我使用的是 Oracle 11g,我的映射是使用通过 Castle Windsor 和 NHibernate 设施注册的 FluentNHibernate 完成的。正如我所写,奇怪的是查询与 QueryOver<> API 完美配合,但不能通过 LINQ。

4

2 回答 2

11

There is an issue with the current implementation of the LINQ extensionmethods for NHibernate 3.1.0.4000 used together with NHibernate Facility 2.0RC (and previous versions) (see: https://nhibernate.jira.com/browse/NH-2626 and discussion here: http://groups.google.com/group/castle-project-devel/browse_thread/thread/ac90148a8d4c8477)

The fix I am using at the moment is to simply ignore the LINQ extensionmethods provided by NHibernate and create it myself. They're really just one-liners:

public static class NHibernateLinqExtensions
{
    /// <summary>
    /// Performs a LINQ query on the specified type.
    /// </summary>
    /// <typeparam name="T">The type to perform the query on.</typeparam>
    /// <param name="session"></param>
    /// <returns>A new <see cref="IQueryable{T}"/>.</returns>
    /// <remarks>This method is provided as a workaround for the current bug in the NHibernate LINQ extension methods.</remarks>
    public static IQueryable<T> Linq<T>(this ISession session)
    {
        return new NhQueryable<T>(session.GetSessionImplementation());
    }

    /// <summary>
    /// Performs a LINQ query on the specified type.
    /// </summary>
    /// <typeparam name="T">The type to perform the query on.</typeparam>
    /// <param name="session"></param>
    /// <returns>A new <see cref="IQueryable{T}"/>.</returns>
    /// <remarks>This method is provided as a workaround for the current bug in the NHibernate LINQ extension methods.</remarks>
    public static IQueryable<T> Linq<T>(this IStatelessSession session)
    {
        return new NhQueryable<T>(session.GetSessionImplementation());
    }
}

Then, when I need to do a LINQ query, I just use session.Linq<EntityType>() instead of session.Query<EntityType>.

Hope it helps someone in the same situation that I was.

于 2011-04-06T08:44:25.580 回答
0

我发现以下内容:http ://groups.google.com/group/castle-project-users/browse_thread/thread/5efc9f3b7b5d6a08

显然,当前版本的 NHibernate Facility 和 NHibernate 3.1.0.4000 存在问题。

我想我只需要等待修复:)

于 2011-04-05T21:00:12.460 回答