NHibernate QueryOver and MYSQL

佐手、 提交于 2019-12-11 19:19:51

问题


Hi I need help with making QueryOver in NHibernate from MYSQL. MYSQL code looks like this

select lietadlo.id,lietadlo.volne,spolocnost.name,typ.name,typ.miest from lietadlo
join spolocnost on spolocnost.id = lietadlo.spolocnostt_id
join typ on typ.id = lietadlo.typp_id
where spolocnost.pocetlietadiel > 2

And then how can i write it in Data Grid View ?

Edit: So I have done so far this and try it(works good)

ISessionFactory factory =
new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
ISession session = null;
session = factory.OpenSession();

Lietadlo f = null;
Spolocnost t = null;
Typ r = null;

dgv.DataSource = session.QueryOver<Lietadlo>(() => f)
.JoinAlias(() => f.Spolocnostt_Id,() => t)
.JoinAlias(() => f.Typp_Id, ()=> r)
.Where(() => t.Pocetlietadiel > 2)
.And(() => r.Name == "Boeing-747")
.List<Lietadlo>()
.ToList<Lietadlo>();

But still in DataGridView I get only columns from Lietadlo and I want from Lietadlo only id(int),volne(int) and from Spolocnost name(string) and from Typ name(string) and miest(int).


回答1:


I'd expect, that this is one way binding (just for reading). In this scenario you could profit from Projections. see more here 16.6. Projections

You can create some DTO Object for your grid and similar to documentation:

CatSummary summaryDto = null;
IList<CatSummary> catReport =
    session.QueryOver<Cat>()
        .SelectList(list => list
            .SelectGroup(c => c.Name).WithAlias(() => summaryDto.Name)
            .SelectAvg(c => c.Age).WithAlias(() => summaryDto.AverageAge))
        .TransformUsing(Transformers.AliasToBean<CatSummary>())
        .List<CatSummary>();

You should be able to do it like this (I cannot check it right now, but it should be clear)

LietadloDTO lietadloDTO = null;
dgv.DataSource = session
  .QueryOver<Lietadlo>(() => f)
  .JoinAlias(() => f.Spolocnostt_Id,() => t)
  .JoinAlias(() => f.Typp_Id, ()=> r)
  .Where(() => t.Pocetlietadiel > 2)
  .And(() => r.Name == "Boeing-747")
  .SelectList(list => list
    .Select(f => f.Id).WithAlias(() => lietadloDTO.Id)
    .Select(t => t.Name).WithAlias(() => lietadloDTO.Name)
    ...
    )
  .TransformUsing(Transformers.AliasToBean<LietadloDTO>())
  .List<LietadloDTO>()
  .ToList<LietadloDTO>();

So, in this case, you will force NHibernate to create Projection (only 1 SELECT clause) and return all needed data at once



来源:https://stackoverflow.com/questions/16496823/nhibernate-queryover-and-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!