Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What Paco said is not right. This can be done in Fluent NHibernate. I searched the web myself quite a while, couldn't find anyone talking about this option, so I just played around with FNHibernate a little and finally managed to do it.</p> <p>This was my scenario :</p> <p>I have two tables -</p> <pre><code>"FormFields" =&gt; Columns { "FieldId", "FieldName", "FieldType", "DisplayOrder" } "FormStructure" =&gt; Columns { "FormId", "FormType", "FieldId" } </code></pre> <p>These were my entities :</p> <pre><code>public class FormStructure { public virtual Int32 FormId { get; private set; } public virtual Int32 FormType { get; set; } public virtual FormField FieldId { get; set; } } public class FormField { public virtual int FieldId { get; private set; } public virtual String FieldName { get; set; } public virtual int? FieldType { get; set; } public virtual int? DisplayOrder { get; set; } } </code></pre> <p>I have a couple of methods in my query that return a list of <code>FormStructure</code> objects. I wanted these methods to give me them ordered by the <code>DisplayOrder</code> field in the <code>FormField</code> object, and wanted the <code>DisplayOrder</code> available as a property in my <code>FormStructure</code> object for other reasons as well.</p> <p>This basically means I needed to join the tables so that I would retrieve all the columns from the FormStructure table along with the <code>DisplayOrder</code> column from the <code>FormField</code> table, joining them on the matching <code>FieldId</code> columns.</p> <p>What I did to solve this :</p> <ol> <li><p>I added a property called DisplayOrder to my <code>FormStructure</code> object.</p> <pre><code>public virtual int? DisplayOrder { get; set; } </code></pre></li> <li><p>I added the <code>Join</code> method to my <code>FormStructure</code> mapping class so it looked like this.</p> <pre><code>public class FormStructureMap : ClassMap&lt;FormStructure&gt; { public FormStructureMap() { Table("FormStructure"); Id(x =&gt; x.Id); Map(x =&gt; x.FormType); References(x =&gt; x.Schedule).Column("ScheduleId"); References(x =&gt; x.Field).Column("FieldId"); Map(x =&gt; x.IsMandatory).Nullable(); Join("FormFields", m =&gt; { m.Fetch.Join(); m.KeyColumn("FieldId"); m.Map(t =&gt; t.DisplayOrder).Nullable(); }); } } </code></pre></li> </ol> <p>The <code>Join</code> method will obviously join between the two tables on the column you defined in the KeyColumn method within the Join.</p> <p>This will also remove some of the rows that have null values. In order to avoid this (I ran into this recently) you can add <code>m.Optional();</code> inside the <code>Join</code> method.</p> <p>I could now retrieve a list of <code>FormStructure</code> objects, order them by <code>DisplayOrder</code> and even have <code>DisplayOrder</code> available as a property in the <code>FormStructure</code> object.</p> <pre><code>return session.CreateCriteria&lt;FormStructure&gt;() .Add(Expression.Eq("FieldName", fieldName)) .AddOrder(Order.Asc("DisplayOrder")) .List&lt;FormStructure&gt;(); </code></pre> <p>This couldn't have been done before, because it wouldn't have recognized the <code>DisplayOrder</code> column in the Order clause I have there.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload