gpt4 book ai didi

NHibernate 2nd lvl 缓存,自定义查询,sqldialect

转载 作者:行者123 更新时间:2023-12-04 00:34:20 27 4
gpt4 key购买 nike

我有 NH 和 FNH 的主干版本。当我尝试添加二级缓存时,NHibernate 的某些部分忘记了选择的 sqldialect。

初始配置:

var cfg = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(connectionString)
.DefaultSchema("dbo")
.UseReflectionOptimizer()
.Mappings(m => ................);

有罪自定义查询:
var sql = @"with Foo(col1,col2,col3)
as (select bla bla bla...)
Select bla bla bla from Foo";

list = Session.CreateSQLQuery(sql)
.AddEntity("fizz", typeof(Fizz))
.SomethingUnimportant();

当我将配置更改为:
var cfg = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(connectionString)
.DefaultSchema("dbo")
.UseReflectionOptimizer()
.Cache(c=>c
.UseQueryCache()
.ProviderClass<HashtableCacheProvider>())
.ShowSql())
.Mappings(m => ................);

查询抛出错误( WITH 在 mssql2008 中添加子句):

The query should start with 'SELECT' or 'SELECT DISTINCT'

[NotSupportedException: The query should start with 'SELECT' or 'SELECT DISTINCT'] NHibernate.Dialect.MsSql2000Dialect.GetAfterSelectInsertPoint(SqlString sql) +179 NHibernate.Dialect.MsSql2000Dialect.GetLimitString(SqlString querySqlString, Int32 offset, Int32 limit) +119 NHibernate.Dialect.MsSql2005Dialect.GetLimitString(SqlString querySqlString, Int32 offset, Int32 last) +127 NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session) +725 NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +352 NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +114 NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +205



任何想法究竟是什么混淆了 nhibernate 以及如何解决它?

有罪的 NHibernate 代码(在 NHibernate/Dialect/MsSql200Dialect.cs 中):
private static int GetAfterSelectInsertPoint(SqlString sql)
{
if (sql.StartsWithCaseInsensitive("select distinct"))
{
return 15;
}
else if (sql.StartsWithCaseInsensitive("select"))
{
return 6;
}
throw new NotSupportedException
("The query should start with 'SELECT' or 'SELECT DISTINCT'");
}
}

看起来 .SetMaxResults(123)导致这个。幸运的是,我可以取消绑定(bind)该查询。

希望这能解决这个问题。

最佳答案

我使用 Alkampfer 的解决方案修复了该错误,但我创建了自己的 SQL 方言,而不是直接修补 NHibernate 源:

public class Sql2008DialectWithBugFixes : MsSql2008Dialect
{
public override SqlString GetLimitString(SqlString querySqlString, int offset, int last)
{
if (offset == 0)
{
return querySqlString.Insert(GetAfterSelectInsertPoint(querySqlString), " top " + last);
}

return base.GetLimitString(querySqlString, offset, last);
}

private static int GetAfterSelectInsertPoint(SqlString sql)
{
Int32 selectPosition;

if ((selectPosition = sql.IndexOfCaseInsensitive("select distinct")) >= 0)
{
return selectPosition + 15; // "select distinct".Length;

}
if ((selectPosition = sql.IndexOfCaseInsensitive("select")) >= 0)
{
return selectPosition + 6; // "select".Length;
}

throw new NotSupportedException("The query should start with 'SELECT' or 'SELECT DISTINCT'");
}
}

关于NHibernate 2nd lvl 缓存,自定义查询,sqldialect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2461658/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com