views:

61

answers:

1

Hi all, I have map the entities in .hmb.xml and define attribute for all entity in classes.

I have some basic accomplishment and get all the record using below code.

public List<DevelopmentStep> getDevelopmentSteps()
   {
       List<DevelopmentStep> developmentStep;
       developmentStep = Repository.FindAll<DevelopmentStep>(new OrderBy("Id", Order.Asc));
       return developmentStep;
   } 

I have check from net that we can write HQL, Now the problem is how to execute this HQL like..

string hql = "From DevelopmentSteps d inner join table2 t2 d.id=t2.Id where d.id=IDValue";

What additional Classes or other thing I need to add to execute this kind of HQL?

Please help me ---- Thanks

+1  A: 
  • To write dynamic queries, I recommend using the Criteria API. This is dynamic, because you have a single query for several different types and you also want to set the ordering dynamically.
  • The queries are always object oriented. You don't need to join by foreign keys, you just navigate through the class model. There also no "tables" in the queries, but entities.
  • Getting (single) instances by ID should always be done using session.Get (or session.Load). Only then NHibernate can also take it directly from the cache without database roundtrip, it it had already been loaded.

for instance:

public IList<T> GetAll<T>(string orderBy)
{
    return session.CreateCriteria(typeof(T))
      .AddOrder(Order.Asc(orderBy))
      .List<T>();
}
Stefan Steinegger
Criteria API, is this .dll I have to add? whats session here and need to pass here?
Muhammad Akhtar
The criteria is included in Nhibernate.dll. Take a look at the manual. http://nhforge.org/doc/nh/en/index.html#manipulatingdata-criteria
Stefan Steinegger
@Stefan; you provide me very good link, I am facing problem, how can create session object to get CreateCriteria method. I am unable to get in my class, can you plz tell me what to do get session.CreateCriteria().......Thanks
Muhammad Akhtar
Thanks..........
Muhammad Akhtar
Can you plz look into this question http://stackoverflow.com/questions/2681078/nhibernate-getting-single-column-from-other-table
Muhammad Akhtar