NHibernate如何将从多个表中提取的SQL查询转换为QueryOver

时间:2013-04-09 12:30:26

标签: sql nhibernate fluent-nhibernate

你好任何NHibernate天才,

我真的很感激一些帮助/指导,我有点卡住了!

我有以下SQL查询,我想将其转换为QueryOver,我不确定天气可以完成吗?任何帮助将不胜感激。

SQL:

Insert Into #categoryAndItems
Select GlobalRateCategoryId,null,[Description],null, null,null, GlobalRateCategoryId, 2 From [GlobalRateCategory]

Insert Into #categoryAndItems
Select Id,Name, gr.[Description],un.[Description],null,gr.Formula,gr.GlobalRateCategoryId, 1  From dbo.[GlobalRateCategoryVariable] gr JOIN 
dbo.UnitOfMeasure un on gr.UnitOfMeasureId = un.UnitOfMeasureId
JOIN [GlobalRateCategory] grc on grc.GlobalRateCategoryId = gr.GlobalRateCategoryId

Insert Into #categoryAndItems
Select gr.GlobalResourceId,null, gr.[Description], um.[Description],    gr.Rate,grr.Formula, c.id, 0 From #categoryAndItems c JOIN 
GlobalRateResource grr on c.id = grr.GlobalRateCategoryId JOIN
GlobalResource gr on grr.GlobalResourceId = gr.GlobalResourceId JOIN 
UnitOfMeasure um on gr.UnitOfMeasureId = um.UnitOfMeasureId

Select * From #categoryAndItems
order by groupid,id
drop table #categoryAndItems

1 个答案:

答案 0 :(得分:1)

这不能转换为QueryOver,但为什么不使用命名查询和XML嵌入资源然后投影到DTO?

var results = Session
  .GetNamedQuery("GetCategoriesAndItems")
  .SetResultTransformer(
    new AliasToBeanResultTransformer(typeof(CategoryAndItemsDto)));
return results.List<CategoryAndItemsDto>();

和你的DTO: -

public class CategoryAndItemsDto{
  public virtual int Id { get; set; }
  public virtual string Description{ get; set; }
 ...
}

这是嵌入式XML文件....

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="GetCategoriesAndItems">
  Insert Into #categoryAndItems
  Select ...

  Insert Into #categoryAndItems
  Select ...

  Insert Into #categoryAndItems
  Select ...

  Select * From #categoryAndItems
  order by groupid,id

  drop table #categoryAndItems
  </sql-query>
</hibernate-mapping>

确保select *返回的ALL列名与DTO中的列完全匹配(记住它区分大小写)

拼图的最后一点告诉流利的你有一个XML文件: -

sessionFactory = Fluently.Configure()
.Mappings(m =>
{
   ...
   m.HbmMappings.AddFromAssemblyOf<SomeEntityMap>();
})
.BuildSessionFactory();

注意:SomeEntityMap是程序集中包含XML嵌入资源的类。