Ok, So I have the following database schema
And the relation is a one-to-many relation (one Album can have many Images)
The mapping looks something like this:
<property name="NumID"...>
<.../>
</property>
<set name="Images" inverse="true" cascade="delete" generic="true">
<key>
<column name="AlbumID" />
</key>
<one-to-many class="Images" />
</set>
And the default fetching is set to lazy
Now to the problem: I want to load the album that holds a picture (to which I know the NumID)
And I would do this with the following SQL:
select * from Images img
join Albums alb
on alb.NumId = img.AlbumId
where img.NumID = 1026
But how would I do it in NHibernate? Of course I have tried in a bunch of different ways with different level of success, but nothing that is as simple as the SQL it selves.
As a aggravating factor I do need the object (Album) as a whole (or at least I need both the NumID and the Name, not lazy loaded)
I have tried this:
/* 1 */
var tmp1 = session.QueryOver<Images>()
.Where(i => i.NumID == ImageID) //The ImageID is for example 1026
.JoinQueryOver(i => i.Album)
.Select(x => x.Album)
.List<Album>();
// Decent to read, but this only loads the NumID from the album
/* 2 */
var tmp2 = session.Query<Images>()
.Join(DBContext.GetQuery<Albums>(), i => i.Albums.NumID, a => a.NumID,
(imgAlias, albAlias) => new {imgAlias, albAlias})
.Where(x => x.imgAlias.NumID == ImageID) //ImageID = 1026
.Select(x => x.albAlias).ToList();
// This one actually works, but it's almost totally unreadable!
How is it supposed to be done?