How to load complete object (that has all asociated objects within) using Fluent NHibernate? In this case the object is named Project and has associated Category and list of Pictures. Mapping file looks like this:
public ProjectMap()
{
Id(x => x.Id);
Map(x => x.Title).Not.Nullable();
Map(x => x.Year);
Map(x => x.Slug).Not.Nullable();
Map(x => x.Description).CustomSqlType("nvarchar(MAX)").Not.Nullable();
References(x => x.Category, "CategoryId").Not.Nullable();
HasMany(x => x.Gallery)
.Inverse()
.Cascade.All();
}
And repository method that should return the complete object:
public Project Read(int id)
{
using (var session = NHibernateHelper.OpenSession())
{
var project = session.CreateQuery("FROM Project WHERE Id = :Id LEFT JOIN FETCH p.Category LEFT JOIN FETCH p.Gallery")
.SetParameter("Id", id).UniqueResult<Project>();
return project;
}
}