NHibernate QueryOver和MYSQL [英] NHibernate QueryOver and MYSQL

查看:165
本文介绍了NHibernate QueryOver和MYSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,从MYSQL在NHibernate中创建QueryOver。 MYSQL代码看起来像这样

  select lietadlo.id,lietadlo.volne,spolocnost.name,typ.name,typ.miest from lietadlo 
加入spolocnost on spolocnost.id = lietadlo.spolocnostt_id
连接typ typedid = lietadlo.typp_id
其中spolocnost.pocetlietadiel> 2

然后我如何在Data Grid View中写?



编辑:所以我已经做到这一点,尝试(工作良好)

  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>();

但是仍然在DataGridView中,我只收到来自Lietadlo的列,我想从Lietadlo只有id(int) volne(int)和Spolocnost名称(string)和Typ name(string)和miest(int)。

解决方案

'd期望,这是一个方式绑定(仅供阅读)。在这种情况下,您可以从投影中获利。在这里查看更多 16.6。投影



您可以为网格创建一些DTO对象,类似于文档:

  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>();

你应该可以这样做这样(我现在不能检查,但是应该清楚)

  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>();因此,在这种情况下,您将强制NHibernate创建Projection(只有1个SELECT子句)并返回所有需要的数据一次


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).

解决方案

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

这篇关于NHibernate QueryOver和MYSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆