问题描述
我对NHibernate有问题,我似乎找不到任何解决方案.在我的项目中,我有一个简单的实体(批处理),但是每当尝试运行以下测试时,都会遇到异常.我尝试了几种不同的方式来执行类似的查询,但是对于所有人而言几乎都是相同的(执行LINQ方法有所不同).
I have a problem with NHibernate, I can't seem to find any solution for.In my project I have a simple entity (Batch), but whenever I try and run the following test, I get an exception.I've triede a couple of different ways to perform a similar query, but almost identical exception for all (it differs in which LINQ method being executed).
第一次测试:
[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<>):
However, this one is passing (using 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.
Using the QueryOver<> API is not bad at all, but I'm just kind of baffled that the Query<> API isn't working, which is kind of sad, since the First() operation is very concise, and our developers really enjoy LINQ.
我真的希望对此有一个解决方案,因为如果这些方法未能通过如此简单的测试,这似乎很奇怪.
I really hope there is a solution to this, as it seems strange if these methods are failing such a simple test.
编辑
我正在使用Oracle 11g,我的映射是通过通过Castle Windsor向NHibernate设施注册的FluentNHibernate完成的.如我所写,奇怪的是该查询可以与QueryOver<> API完美配合,而不能通过LINQ.
I'm using Oracle 11g, my mappings are done with FluentNHibernate registered through Castle Windsor with the NHibernate Facility.As I wrote, the odd thing is that the query works perfectly with the QueryOver<> API, but not through LINQ.
推荐答案
与NHibernate Facility 2.0RC(及以前的版本)结合使用的NHibernate 3.1.0.4000的LINQ扩展方法的当前实现存在问题. a href ="https://nhibernate.jira.com/browse/NH-2626" rel ="nofollow"> https://nhibernate.jira.com/browse/NH-2626 并在此处进行讨论: http://groups.google.com/group/castle-project- devel/browse_thread/thread/ac90148a8d4c8477 )
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)
我目前使用的解决方法是简单地忽略NHibernate提供的LINQ扩展方法,并自己创建它.它们实际上只是一线:
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());
}
}
然后,当我需要执行LINQ查询时,我只使用session.Linq<EntityType>()
而不是session.Query<EntityType>
.
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.
这篇关于使用Query的NHibernate(3.1.0.4000)NullReferenceException;和NHibernate设施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!