NHibernate 无法使用 containstable() 查询的结果

发布于 2024-11-25 17:55:00 字数 965 浏览 2 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海夕 2024-12-02 17:55:00

您需要告诉 NH 这些行代表什么。

session.CreateSQLQuery(...)
       .AddEntity(typeof(Article))
       .SetParameter(...)
       .List<Article>();

阅读第 16 章本机 SQL 了解更多详细信息

You need to tell NH what those rows represent.

session.CreateSQLQuery(...)
       .AddEntity(typeof(Article))
       .SetParameter(...)
       .List<Article>();

Read Chapter 16. Native SQL for more details

飘过的浮云 2024-12-02 17:55:00

如果您直接作为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文