我正在使用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)
可能是什么问题? ----谢谢
答案 0 :(得分:3)
你从根本上误解了NHibernate。 NHibernate与TypeDataSource类不同,它返回的是不是真正业务对象的DataSets / DataTable。
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作为参考。
答案 1 :(得分:0)
答案 2 :(得分:0)
在大多数情况下,您应该使用实体对象而不是自定义查询。 如果您确实需要自定义查询,则以下示例可能很有用
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; }
}