Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate and parent/child collection mapping
    text
    copied!<p>I have this table containing both parent and child elements. </p> <pre><code>CREATE TABLE Expenses( [BudgetId] int, [AccountGroupId] int, [AccountNumber] int, [Amount] decimal ) </code></pre> <p>In my domain model it's represented by this hierachy:</p> <ul> <li>Budget <ul> <li>AccountGroup <ul> <li>ExpenseLine</li> <li>ExpenseLine</li> <li>ExpenseLine</li> </ul></li> </ul></li> </ul> <p>So a Budget has a collection of AccountGroups and each AccountGroup has a collection of ExpenseLines.</p> <p>If a row in the Expenses table has AccountNumber = 0 it's an AccountGroup (parent), and otherwise it's an ExpenseLine (child of AccountGroup).</p> <p>I'm trying to express this relationship in a mapping file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;class name="Budget, MyCompany.Budget" table="Expenses"&gt; &lt;composite-id&gt; &lt;key-property name="AccountGroupId" column="AccountGroupId" /&gt; &lt;/composite-id&gt; &lt;property name="BudgetId" column="BudgetId" type="int" not-null="true" /&gt; &lt;property name="AccountNumber" column="AccountNumber" type="int" not-null="true" /&gt; &lt;property name="Amount" column="Amount" /&gt; &lt;bag name="ExpenseLines" lazy="false" where="AccountNumber &gt; 0 AND BudgetId = 7"&gt; &lt;key column="AccountGroupId" /&gt; &lt;one-to-many class="ExpenseLine, MyCompany.Budget" /&gt; &lt;/bag&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>The mapping is working as far as I do get the parent child hierachy loaded into my model, but for now I have to use a specific BudgetId to avoid fetching all ExpenseLines (hence AND BudgetId = 7)</p> <p>How can I express this in my mapping file, so that the ExpenseLines only fetches those rows that matches the current BudgetId?</p> <p>In other words - how do I get rid of the "AND BudgetId = 7" part :)</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