I'm currently working on writing a very basic online forum, and I want to retrieve a thread with a child collection of paged posts. So my mappings are:
<class name="Thread" table="ForumThreads">
<id name="Id">
<generator class="identity"></generator>
</id>
<property name="Title"></property>
<bag name="Posts">
<key column="ThreadID"></key>
<one-to-many class="Post"/>
</bag>
</class>
<class name="Post" table="ForumPosts">
<id name="Id">
<generator class="identity"></generator>
</id>
<property name="Content"></property>
<many-to-one name="Thread"
class="Thread"
column="ThreadID">
</many-to-one>
</class>
And I want to do something like this:
public class Thread
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual IEnumerable<Post> Posts { get; set; }
}
public class Post
{
public virtual int Id { get; set; }
public virtual Thread Thread { get; set; }
public virtual string Content { get; set; }
}
public Thread GetThread(int threadId, int page, int pageSize, out int count)
{
var session = SessionFactory.CurrentSession;
// Query to get the thread with a child collection of paged posts.
return thread;
}
Is it possible to do this with one query, or am I going to have to split it in to two?
Thanks
var threadId = ...
session.QueryOver<Thread>
.Where(thread => thread.Id == threadId)
.Fetch(thread => thread.Posts).Eager
.Take(pageSize)
.Skip(page)
.TransformUsing(Transformers.DistinctRootEntity)
.List<Thread>();