nhibernate joinqueryover unrelated tables (same foreign key)

怎甘沉沦 提交于 2019-12-23 18:45:59

问题


Basically what i am trying to do is join two tables on foreign keys. I have this query:

        var result =
            _session.QueryOver(() => contentReferenceAlias)
                    .Inner.JoinAlias(() => contentReferenceAlias.ContentReference, () => contentLibrarySearchAlias.ContentReference)
                    .Where(() => contentReferenceAlias.ToLanguage.Id == languageId && contentReferenceAlias.ContentReference.Id == contentLibrarySearchAlias.ContentReference.Id)
                    .SelectList(list => list
                                            .Select(() => contentReferenceAlias.ContentReference)
                                            .Select(() => contentLibrarySearchAlias.ContentReference)
                                            .Select(() => contentReferenceAlias.ContentReference.Id).WithAlias(() => resultAlias.ContentReferenceId)
                                            .Select(() => contentReferenceAlias.ContentReference.Id).WithAlias(() => resultAlias.ContentReferenceId)
                                            .Select(() => contentReferenceAlias.OrderedFrom).WithAlia

The SQL im trying to recreate:

  SELECT A.OrderedFrom, C.LastOrdered, A.ContentReferenceId, B.Title FROM TranslationContentReference A
  INNER JOIN TranslationOrder C ON (A.TranslationOrderId = C.Id)
  INNER JOIN ContentLibrarySearch B ON (A.ContentReferenceId = b.ContentReferenceId)
  WHERE A.ToLanguageId = 'xxxx-xxxx-xxxx-xxxx-xxxx'

回答1:


If I do understand your scenario correctly, join over man-in-the-middle (foreign key reference) cannot be achieved via QueryOver API. NHibernate needs to know pathes all the way down, So if there is no explicit mapping from TranslationContentReference through ContentReference to ContentLibrarySearch, then we cannot create correct JoinAliases.

So, first option is to extend the man-in-the-middle object

public class ContentReference
{
    ...
    public virtual IList<TranslationContentReference> TranslationContentReference { get; set;}
    public virtual IList<ContentLibrarySearch> ContentLibrarySearch { get; set;}
}

Then we can navigate (create pathes)

  • from TranslationContentReference to ContentReference
  • from ContentReference to ContentLibrarySearch

The second option, which is less NHibernate and more SQL, is to create ISQLQuery

ISQLQuery query = session.CreateSQLQuery(
  "SELECT A.OrderedFrom, C.LastOrdered, A.ContentReferenceId, B.Title 
    FROM TranslationContentReference A
     INNER JOIN TranslationOrder C ON (A.TranslationOrderId = C.Id)
     INNER JOIN ContentLibrarySearch B ON (A.ContentReferenceId = b.ContentReferenceId)
    WHERE A.ToLanguageId = :language");
query.SetString("language", "xxxx-xxxx-xxxx-xxxx-xxxx");
var result = query.SetResultTransformer(new AliasToBeanResultTransformer(typeof(MyDTO)))
.List();


来源:https://stackoverflow.com/questions/13804204/nhibernate-joinqueryover-unrelated-tables-same-foreign-key

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