使用查询的NHibernate(3.1.0.4000)NullReferenceException<&燃气轮机;和NHibernate设施

使用查询的NHibernate(3.1.0.4000)NullReferenceException<&燃气轮机;和NHibernate设施,nhibernate,oracle11g,castle-windsor,linq-to-nhibernate,windsor-nhfacility,Nhibernate,Oracle11g,Castle Windsor,Linq To Nhibernate,Windsor Nhfacility,我对NHibernate有问题,我似乎找不到任何解决方案。 在我的项目中,我有一个简单的实体(批处理),但是每当我尝试运行下面的测试时,我都会得到一个异常。 我尝试了两种不同的方法来执行类似的查询,但几乎所有的查询都有相同的异常(执行LINQ方法的方式不同) 第一个测试: [Test] public void QueryLatestBatch() { using (var session = SessionManager.OpenSession()) { var

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

第一个测试:

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

        Assert.That(batch, Is.Not.Null);
    }
}
第二个测试:

[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);
    }
}
但是,这一个正在传递(使用QueryOver):

[测试]
public void QueryOverLatestBatch()
{
使用(var session=SessionManager.OpenSession())
{
var batch=session.QueryOver()
.OrderBy(x=>x.Executed).Asc
.采取(1)
.SingleOrDefault();
Assert.That(批处理,Is.Not.Null);
Assert.That(batch.Executed,Is.LessThan(DateTime.Now));
}
}
使用QueryOver API一点也不差,但我只是有点困惑,查询API不起作用,这有点悲哀,因为第一个()操作非常简洁,我们的开发人员非常喜欢LINQ

我真的希望有一个解决办法,因为如果这些方法在这样一个简单的测试中失败,这似乎很奇怪

编辑

我使用的是Oracle11g,我的映射是通过CastleWindsor注册的FluentNHibernate完成的,NHibernate设施。 正如我所写的,奇怪的是,查询与QueryOver API完美配合,但不是通过LINQ。我发现:

显然,NHibernate设施和NHibernate 3.1.0.4000的当前版本存在问题


我想我只能等待修复:)

与NHibernate Facility 2.0RC(和以前的版本)一起使用的NHibernate 3.1.0.4000的LINQ扩展方法的当前实现存在问题(请参阅:和此处的讨论:)

我目前使用的修复方法是忽略NHibernate提供的LINQ扩展方法,自己创建它。它们实际上只是一句俏皮话:

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());
    }
}
公共静态类nhibernatelinkExtensions
{
/// 
///对指定类型执行LINQ查询。
/// 
///要对其执行查询的类型。
/// 
///一个新的。
///此方法是作为NHibernate LINQ扩展方法中当前错误的解决方法提供的。
公共静态IQueryable Linq(此会话会话)
{
返回新的NhQueryable(session.GetSessionImplementation());
}
/// 
///对指定类型执行LINQ查询。
/// 
///要对其执行查询的类型。
/// 
///一个新的。
///此方法是作为NHibernate LINQ扩展方法中当前错误的解决方法提供的。
公共静态IQueryable Linq(此IStatelessSession会话)
{
返回新的NhQueryable(session.GetSessionImplementation());
}
}
然后,当我需要执行LINQ查询时,我只使用
session.LINQ()
而不是
session.query


希望它能帮助与我处境相同的人。

您的映射、配置或未显示的代码的其他部分有问题,因为所有这些方法对我来说都很好(在NH测试中),我的设置有严重问题。我现在无法让任何查询方法工作。我想我的推荐信有问题。我发现了问题所在。很明显,我在NHibernatefacity做了些错事,因为这就是造成所有问题的原因。如果我在设施外创建了一个会话工厂,我可以使用query进行查询,但是使用它,它不起作用!
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)
[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));
    }
}
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());
    }
}