I have created a generic function for nHibernate to convert List to Bean which works fine however I need a way to do conversion in a generic way without calling AddScalar(as commented in my code) to my query that is assume I have over 50 model pojo and want to use same generic method to pull list of records how can I achieve this. This is my generic Method that I want to use to any model and pass associated query.
- public static IList
getRawQuery string query)//( - {
- IList
ls = new List (); - ISession session = MyHibernateUtil.openSession();
- try
- {
- ls = session.CreateSQLQuery(@query)
- .AddScalar("total", NHibernate.NHibernateUtil.Int32)//dont want to do this
- .AddScalar("product_name", NHibernate.NHibernateUtil.String)//this will force me to have several methods doing same work
- .AddScalar("barcode", NHibernate.NHibernateUtil.String)
- .SetResultTransformer(Transformers.AliasToBean(typeof(T)))
- .List
(); - }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- }
- finally
- {
- MyHibernateUtil.CloseSession(session);
- }
- return ls;
- }
and here is my one of my model
- public class SalesModel
- {
- public virtual String barcode { get; set; }
- public virtual String product_name { get; set; }
- public virtual int total { get; set; }
- }
Replies
Know the answer? Post it — somebody with the same question will find it here.