[英]Nhibernate - Getting Exception executing SQL Query
I am executing a SQL Query using Nhibernate, below is the code in which I use for this: 我正在使用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;
}
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 - - 谢谢
You are fundamentally misunderstanding NHibernate. 你从根本上误解了NHibernate。 NHibernate is not like the TypeDataSource classes that return you DataSets/DataTables that aren't real business objects. NHibernate与TypeDataSource类不同,它返回的是不是真正业务对象的DataSets / DataTables。
NHibernate is meant to work with fully owned objects so you would have something similar to NHibernate旨在使用完全拥有的对象,因此您可以使用类似的东西
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. 然后,您需要手动或通过代码生成创建映射文件以用于原始HBM映射,或者使用NH之上的工具以编程方式使用FluentNHibernate或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 在尝试查询之前,您需要学习NHibernate的基础知识,这是一个不错的介绍性帖子: 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. 然后查询您可以使用http://www.castleproject.org/ActiveRecord/documentation/v1rc1/usersguide/hql.html作为参考。
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.