Search code examples
c#nhibernatequeryover

NHibernate left join a unrelated entity


I have two entities:

Konto
- Id
- MwstId

Mwst
- Id
- Code

Both entities are correct mapped in Nhibernate - but the Relation between Konto.MwstId -> Mwst.Id is not mapped. I cannot add this map because of structure restrictions.

Now, I try to implement the following Query with NHibernate-QueryOver-API:

select * from Konto k left outer join Mwst m on k.MwstId = m.Id

I have tried several approaches, but without any success.


Solution

  • You can do it with Criteria/QueryOver or with SQL. If you want to bring back entities with a SQL query, use a syntax like this:

    session.CreateSQLQuery("select {k.*} from Konto k ...").AddEntity("k", typeof(Konto)).List<Konto>();