Wednesday, June 5, 2013

NHibernate: Returning extra properties from stored procedure not in model

I was working on getting the data from two tables using a stored procedure in which resultset was a combination of fields coming from multiple tables. I created the domain model for one of the tables involved in the query in stored procedure and introduced the other fields pertaining to the other table in the same domain model class. I did provide the return property tag in the .hbm mapping file too, as shown below.  

After doing all this I got an error for the fields which were part of the other table for which only properties were added to the domain model class.
Solution:

 
UserData Class
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Reflection;
using NameSpace.DAL.Base;
using NameSpace.DomainModel.Base;
using NameSpace.DomainModel.User;
using NHibernate;
using NHibernate.Criterion;

namespace NameSpace.DAL.User
{
    ///
    /// UserData class for doing DB operations related to User
    ///

    public partial class UserData : DataManagerBase
    {
        #region Constructors

        internal UserData()
            : base()
        {
        }

        ///
        /// Initializes a new instance of the class.
        ///

        /// The session.
        public UserData (INHibernateSession session)
            : base(session)
        {
        }
        #endregion Constructors

        #region Get Methods
        ///
        /// Method to get the User Info.
        ///

        ///
        ///
        public IList GetUserInfo()
        {
            IList lstUserInfo = null;
         
            try
            {
                IQuery Q = GetNamedQuery("User.GetUsers");
                lstUserInfo = Q.List();
             }
            catch (Exception ex)
            {
                 Your Exception Handling Code here.
            }

            return lstUserInfo ;
        }

 #endregion Get Methods
    }
}


User DomainModel Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Namespace.DomainModel.User
{
    ///
    /// Represents a User entity
    ///

    /// XYZ
    /// ABC
    /// TRT
    /// 05 June 2013

    public class User : EntityBase
    {
        #region Constructors

        ///
        /// Class constructor...
        ///

        public User()
        {
            //TODO:Write constructor stuff here..
        }

        #endregion Constructors

        #region Property Declarations

        public virtual int UserId
        {
            get
            {
                return base.Id;
            }
            set
            {
                base.Id = value;
            }
        }

        public virtual string FirstName { set; get; }

        public virtual string LastName { set; get; }

        public virtual string SearchCriterion { set; get; }
      
        #endregion Property Declarations

        #region ToString() Implementation

        ///
        /// This methods returns a string representation of this class.
        ///

        /// A string representation of this class.
        public virtual new string ToString()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(6);

            sb.AppendLine("------------User dump starts---------------");
            sb.AppendLine("UserId: " + (this.Id.ToString()));
            sb.AppendLine("FirstName: " + (this. FirstName != null ? this. FirstName: "").ToString());
            sb.AppendLine("LastName: " + (this. LastName!= null ? this. LastName: "").ToString());
            sb.AppendLine("SearchCriterion: " + (this. SearchCriterion!= null ? this. SearchCriterion : "").ToString());
sb.AppendLine("------------User dump ends---------------");
            return sb.ToString();
        }

        public override bool Equals(object obj)
        {
            //Same reference check !
            if (object.ReferenceEquals(this, obj)) return true;

            //Type check !
            User other = obj as User;
            if (other == null) return false;

            if ((Id == 0))
                //Do a per-property search
                return
                (FirstName == other.FirstName) &&
                (FirstName == other.FirstName) &&
                (SearchCriterion == other.SearchCriterion) ;
            else
                return Id == other.Id;
        }

        public override int GetHashCode()
        {
            return (this.GetType().FullName + Id.ToString()).GetHashCode();
        }

        #endregion ToString() Implementation
    }
}