In brief: query to "parents" table results "parents" + records from "children". "children" have FKs to "parents" and therefore I have as so many duplictes as quantity of FKs in "children". I'm using Fluent.NHibernate in ASP.NET MVC3 project.
So, DB looks like this
TABLE parents
***id
***system_name
TABLE children
***id
***custom_name
***parent_id
FK: parent_id === "FK_parent_id_to_id_in_parents"
Model:
public class Parent
{
public virtual int Id { get; set; }
public virtual string SystemName { get; set; }
}
public class Child: Parent
{
public override int Id
{
get
{
return base.Id;
}
set
{
base.Id = value;
}
}
public virtual string CustomName
{
get
{
return _CustomName != null ? _CustomName : base.SystemName;
}
set
{
_CustomName = value;
}
}
public virtual Parent ParentId { get; set; }
private string _CustomName = string.Empty;
}
Mappings:
public class ParentMapper: ClassMap<Parent>
{
Table("parents");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.SystemName, "system_name").Not.Nullable();
}
public class ChildMapper: ClassMap<Child>
{
Table("children");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.DisplayName, "display_name");
References(x => x.ParentId).Column("parent_id").Cascade.None().ForeignKey("FK_parent_id_to_id_in_parents");
}
Query:
using(ITransaction tr = session.BeginTransaction())
{
List<Parent> lp = new List<Parent>(session.CreateCriteria(typeof(Parent)).List<Parent>());
tr.Commit();
}