NHibernate 无法使用 containstable() 查询的结果
我正在将 LINQ2SQL 项目转换为使用 NHibernate。我无法有效移植的一个查询是全文搜索。以前我使用存储过程来完成搜索,但现在我直接将其作为查询访问。 SQL 查询是:
select a.id, a.parentID, a.text, a.summary
from articles a
inner join containstable(articles, (summary, text), :query) ar on
a.id = ar.[KEY]
order by ar.RANK desc
当我在 SSMS 中运行查询时,我得到了我期望的结果 - 只是 articles
表的匹配项。然而,NHibernate 似乎无法解释结果。我查询的代码是:
IQuery q = session.CreateSQLQuery(
"select a.id, a.parentID, a.[text], a.summary from articles a "
+ "inner join containstable(articles, (summary, text), :query) ar on "
+ "a.id = ar.[KEY] "
+ "order by ar.RANK desc")
.SetParameter<string>("query", query);
var results = q.List<article>();
NHibernate 抛出一个错误,System.Object[]
无法转换为我的 POCO 类型。我尝试了非通用的 List()
方法,发现该列表中的每个项目都是一个对象数组。表上的其他查询工作正常,但这是唯一的 SQL 查询(其他查询都是 Criteria 或 QueryOver)。为什么这个查询不起作用?
I'm in the process of converting a LINQ2SQL project to use NHibernate. One query that I have not been able to port effectively is a full-text search. Previously I was using a stored procedure to accomplish the search but now I'm accessing it as a query directly. The SQL query is:
select a.id, a.parentID, a.text, a.summary
from articles a
inner join containstable(articles, (summary, text), :query) ar on
a.id = ar.[KEY]
order by ar.RANK desc
When I run the query in SSMS I get the results I expect - just the matching items of the articles
table. However, NHibernate does not seem to be able to interpret the results. The code I query with is:
IQuery q = session.CreateSQLQuery(
"select a.id, a.parentID, a.[text], a.summary from articles a "
+ "inner join containstable(articles, (summary, text), :query) ar on "
+ "a.id = ar.[KEY] "
+ "order by ar.RANK desc")
.SetParameter<string>("query", query);
var results = q.List<article>();
NHibernate throws an error that System.Object[]
cannot be converted to my POCO type. I tried the non-generic List()
method and discovered that each item in that list was an array of objects. Other queries on the table work fine, however this is the only SQL query (everything else is Criteria or QueryOver). Why won't this query work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要告诉 NH 这些行代表什么。
阅读第 16 章本机 SQL 了解更多详细信息
You need to tell NH what those rows represent.
Read Chapter 16. Native SQL for more details
如果您直接作为 SQL 运行查询,那么即使表已映射,NHibernate 将不知道如何将其映射到 POCO。您需要向 NHibernate 提供一些有关如何映射 SQL 投影的信息。
如果您可以选择,请使用 HQL 或 LinqToNHibernate。
If you are running the query directly as SQL then NHibernate will have no knowledge of how to map it to your POCOs even if the tables are mapped. You need to supply NHibernate some information about how the SQL projection should be mapped.
Use HQL or LinqToNHibernate if this is an option open to you.