开发者

Nhibernate -- Excuting some simple HQL

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


  • 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>();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜