Search code examples
c#asp.netnhibernatefluent-nhibernatesql

Nhibernate - Getting Exception executing SQL Query


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


Solution

  • 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.