Nhibernate - 执行 SQL 查询时出现异常

发布于 2024-08-29 20:39:54 字数 1233 浏览 3 评论 0原文

我正在使用 Nhibernate 执行 SQL 查询,下面是我用于此目的的代码:

 public ArrayList getDocumentsForApproval(string ReleaseId)
    {
        string query = string.Format("SELECT distinct doc.Id, doc.Name as Doc, doc.url as url, suser.Name as Author, ds.name, CONVERT(VARCHAR(11), doc.DateEntered, 101) as DateEntered FROM dbo.Documents doc INNER JOIN DevelopmentSteps ds ON doc.TypeId = ds.Id INNER JOIN DocumentTrackingItems dti ON doc.Id = dti.DocumentId INNER JOIN TrackingItems ti ON dti.ItemStepId = ti.Id INNER JOIN dbo.Releases rl ON ti.ReleaseId =  rl.BugTrackerName left outer join (select * from users) as suser on doc.AuthorUserid = suser.Id WHERE doc.DateEntered IS NOT NULL AND doc.DateApproved IS NULL AND rl.ID = '{0}'", ReleaseId);
        ISession session = NHibernateHelper.GetCurrentSession();
        ArrayList document =(ArrayList) session.CreateSQLQuery(query).List();
        return document;
    }

我收到的错误信息如下:

**Exception Details:**
NHibernate.QueryException: Return types of SQL query were not specified [SELECT      distinct doc.Id, doc.Name as Doc, doc.url as url, suser.Name as Author, ds.name, CONVERT(VARCHAR(11), doc.DateEntered, 101) 

可能是什么问题? - - 谢谢

I am executing a SQL Query using Nhibernate, below is the code in which I use for this:

 public ArrayList getDocumentsForApproval(string ReleaseId)
    {
        string query = string.Format("SELECT distinct doc.Id, doc.Name as Doc, doc.url as url, suser.Name as Author, ds.name, CONVERT(VARCHAR(11), doc.DateEntered, 101) as DateEntered FROM dbo.Documents doc INNER JOIN DevelopmentSteps ds ON doc.TypeId = ds.Id INNER JOIN DocumentTrackingItems dti ON doc.Id = dti.DocumentId INNER JOIN TrackingItems ti ON dti.ItemStepId = ti.Id INNER JOIN dbo.Releases rl ON ti.ReleaseId =  rl.BugTrackerName left outer join (select * from users) as suser on doc.AuthorUserid = suser.Id WHERE doc.DateEntered IS NOT NULL AND doc.DateApproved IS NULL AND rl.ID = '{0}'", ReleaseId);
        ISession session = NHibernateHelper.GetCurrentSession();
        ArrayList document =(ArrayList) session.CreateSQLQuery(query).List();
        return document;
    }

The error information I receive is as follows:

**Exception Details:**
NHibernate.QueryException: Return types of SQL query were not specified [SELECT      distinct doc.Id, doc.Name as Doc, doc.url as url, suser.Name as Author, ds.name, CONVERT(VARCHAR(11), doc.DateEntered, 101) 

What could be the issue? ---- Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

情域 2024-09-05 20:39:54

你从根本上误解了NHibernate。 NHibernate 与 TypeDataSource 类不同,它返回的数据集/数据表不是真正的业务对象。

NHibernate 旨在处理完全拥有的对象,因此您将拥有类似的内容

Public Class Document
{

    public virtual decimal Id { get; set; }
    public virtual string Name { get; set; }
    public virtual DateTime DateEntered { get; set; }
    ... so forth  
}

然后您需要手动或通过原始 HBM 映射的代码生成创建映射文件,或者使用 NH 之上的工具通过 FluentNHibernate 或 ConfORM 以编程方式构建映射。

在尝试查询之前,您需要学习 NHibernate 的基础知识,这是一篇不错的介绍性文章: http ://www.fincher.org/tips/Languages/NHibernate.shtml

然后可以使用http://www.castleproject.org/ActiveRecord/documentation/v1rc1/usersguide/hql.html 供参考。

You are fundamentally misunderstanding NHibernate. NHibernate is not like the TypeDataSource classes that return you DataSets/DataTables that aren't real business objects.

NHibernate is meant to work with fully owned objects so you would have something similar to

Public Class Document
{

    public virtual decimal Id { get; set; }
    public virtual string Name { get; set; }
    public virtual DateTime DateEntered { get; set; }
    ... so forth  
}

Then you need to create a mapping file either manually or by code generation for raw HBM mappings or use a tool on top of NH to build mappings programmatically with FluentNHibernate or ConfORM.

You need to learn the basics of NHibernate before attempting to query this is a decent introductory post: http://www.fincher.org/tips/Languages/NHibernate.shtml

And then for querying you can use http://www.castleproject.org/ActiveRecord/documentation/v1rc1/usersguide/hql.html for reference.

七禾 2024-09-05 20:39:54

在大多数情况下,您应该使用实体对象而不是自定义查询。
如果您确实需要自定义查询,以下示例可能有用

    public IEnumerable<GeoAreaIdAndCode> ReadAllGssCodes()
    {
        var query = "select GeoAreaID,Code from GeoAreaAlternativeCode where AlternativeCodeType=" + (int)GeoAreaAlternativeCodeType.GssCode;
        var result = Owner.Session.CreateSQLQuery(query)
                                .AddScalar("GeoAreaID",NHibernateUtil.Int32)
                                .AddScalar("Code",NHibernateUtil.String)
                                .SetResultTransformer(Transformers.AliasToBean(typeof (GeoAreaIdAndCode)))
                                .List<GeoAreaIdAndCode>();

        return result;
    }

    public class GeoAreaIdAndCode
    {
        public int GeoAreaID { get; set; }
        public string Code { get; set; }
    }

In most cases you should use entity objects instead of custom queries.
If you really need a custom query, the following example might be useful

    public IEnumerable<GeoAreaIdAndCode> ReadAllGssCodes()
    {
        var query = "select GeoAreaID,Code from GeoAreaAlternativeCode where AlternativeCodeType=" + (int)GeoAreaAlternativeCodeType.GssCode;
        var result = Owner.Session.CreateSQLQuery(query)
                                .AddScalar("GeoAreaID",NHibernateUtil.Int32)
                                .AddScalar("Code",NHibernateUtil.String)
                                .SetResultTransformer(Transformers.AliasToBean(typeof (GeoAreaIdAndCode)))
                                .List<GeoAreaIdAndCode>();

        return result;
    }

    public class GeoAreaIdAndCode
    {
        public int GeoAreaID { get; set; }
        public string Code { get; set; }
    }
煮茶煮酒煮时光 2024-09-05 20:39:54

秘诀在于使用:

CreateSQLQuery("Your query with alias").AddScalar(...)

AddScalar 中,您必须定义输出的 NH 类型。

请参阅此处

The secret is to use:

CreateSQLQuery("Your query with alias").AddScalar(...)

In AddScalar you have to define your NH types for output.

See ref here

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