TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Denis Morgan
NA
72
22.6k
Nhibernate generic method to Query different Model
Sep 7 2020 11:57 PM
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<T> getRawQuery<T>(
string
query)
//
{
IList<T> ls =
new
List<T>();
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<T>();
}
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
; }
}
Reply
Answers (
0
)
Sweet Aleart Success Message.
Convert List to IList<T>