views:

62

answers:

0

Hi,

I have this table containing both parent and child elements.

CREATE TABLE Expenses(
    [BudgetId] int,
    [AccountGroupId] int,
    [AccountNumber] int,
    [Amount] decimal
)

In my domain model it's represented by this hierachy:

  • Budget
    • AccountGroup
      • ExpenseLine
      • ExpenseLine
      • ExpenseLine

So a Budget has a collection of AccountGroups and each AccountGroup has a collection of ExpenseLines.

If a row in the Expenses table has AccountNumber = 0 it's an AccountGroup (parent), and otherwise it's an ExpenseLine (child of AccountGroup).

I'm trying to express this relationship in a mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Budget, MyCompany.Budget" table="Expenses">
    <composite-id>
      <key-property name="AccountGroupId" column="AccountGroupId" />
    </composite-id>
    <property name="BudgetId" column="BudgetId" type="int" not-null="true" />
    <property name="AccountNumber" column="AccountNumber" type="int" not-null="true" />
    <property name="Amount" column="Amount" />

    <bag name="ExpenseLines" lazy="false" where="AccountNumber > 0 AND BudgetId = 7">
      <key column="AccountGroupId" />
      <one-to-many class="ExpenseLine, MyCompany.Budget" />
    </bag>  
  </class>
</hibernate-mapping>

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)

How can I express this in my mapping file, so that the ExpenseLines only fetches those rows that matches the current BudgetId?

In other words - how do I get rid of the "AND BudgetId = 7" part :)