NHibernate Custom IProjection that Runs a Sub-Query

我只是一个虾纸丫 提交于 2019-12-24 14:27:43

问题


Say I have 2 tables as follows:

MainTable (
    Id int,
    Name varchar(100)
)

RelatedTable (
    Id int,
    Value int,
    MainTableId int -- foreign key
)

There is a one-to-many relationship between MainTable and RelatedTable, such that RelatedTable.MainTableId references MainTable.Id.

I want to make a custom IProjection to be used as follows:

sess.CreateCriteria<MainTable>()
    .SetProjection(
        Projections.ProjectionList()
            .Add(Projections.Property("Id"))
            .Add(Projections.Property("Name"))
            .Add(new SumOfValuesProjection(/* arguments??? */))
    )
    .List();

That generates the following SQL:

select
    Id,
    Name,
    -- how to get this sub-query from IProjection?
    (select sum(Value)
    from RelatedTable
    where RelatedTable.MainTableId = MainTable.Id) as SumOfValues
from MainTable

This is just a small example of what I am trying to do. In my case, there could be dozens of these sub-query columns. They all use aggregate functions, but may not all use sum().

I am looking to create a custom IProjection but am not quite sure where to start.

Any help would be greatly appreciated.


回答1:


Maybe not the exact answer, but maybe more direct one. I would like to show, how to use current NHibernate features to execute similar SQL as needed.

The trick won't be in the custom IProjection, but in the call to already existing powerful IProjection implementation: Projections.SubQuery

var criteria = CreateCriteria<MainTable>("Main")
    .SetProjection(
        Projections.ProjectionList()
            .Add(Projections.Property("Id"))
            .Add(Projections.Property("Name"))

            // here we go, let's profit from existing IProjection
            .Add(Projections.SubQuery(
                   DetachedCriteria
                       .For<RelatedTable>("Related")
                       .SetProjection(Projections.Sum("Value"))
                       .Add(Restrictions.EqProperty("Main.Id", "Related.MainTableId")))
                   , "value")
                )
     ...
     ;

In case that this approach is not enough, I would suggest to observe how this functionality is implemented in current NHibernate code. Because still - NHibernate is Open Source ...



来源:https://stackoverflow.com/questions/18970893/nhibernate-custom-iprojection-that-runs-a-sub-query

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