Filip
Filip

Reputation: 652

NHibernate mapping problem

I can't figure out how to get my mappings correct with Nhibernate 3.0

I have have two POCOs:

public class Account
{
    public virtual string AccountNumber { get; set; }

    public virtual IList<AccountSummary> AccountSummaries { get; set; }
}

public class AccountSummary
{
    public virtual uint Id { get; protected set; }

    public virtual string AccountNumber { get; set; }

    public virtual DateTime TradeDate { get; set; }
}

I want to map the classes in tables like this:

Table: accounts (column: account_number)

Table: account_summaries(column: id, column: trade_date, column: account_number)

How can I avoid having a Account object in the AccountSummary class, as I normally would do do with a one-to-many/many-to-one mapping. Instead of my AccountSummary class having a Account object I want the AccountSummary class to have a AccountNumber property as in the class definition with a reference to the Account class.

I would also like to map the trade_date and account_number columns as natural-id or unique-key.

My mappings look like this:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                           assembly="FundAccountDBExperiment"
                           namespace="FundAccountDBExperiment.Models">
  <class name="Account" table="accounts">
    <composite-id>
      <key-property name="AccountNumber" column="account_number" length="40"/>
    </composite-id>
    <bag name="AccountSummaries" table="account_summaries" cascade="all-delete-orphan" inverse="true">
      <key column="account_number"/>
      <one-to-many class="AccountSummary"/>
    </bag>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                            assembly="FundAccountDBExperiment"
                            namespace="FundAccountDBExperiment.Models">
  <class name="AccountSummary" table="account_summaries">
    <id name="Id" column="id">
      <generator class="native"/>
    </id>
    <property name="TradeDate" column="trade_date" type="date" not-null="true"/>
    <join table="account">
      <key column="acount_number"/>
    </join>
  </class>
</hibernate-mapping>

Any kind of help would be appreciated.

Upvotes: 0

Views: 635

Answers (1)

Firo
Firo

Reputation: 30813

wouldn't this suffice?

public class AccountSummary
{
    public virtual uint Id { get; protected set; }

    public virtual Account Account { get; set; }

    public virtual string AccountNumber
    {
        get { return Account.AccountNumber; }
        set { Account.AccountNumber = value; }
    }

    public virtual DateTime TradeDate { get; set; }
}

and the unique constraint see "myUniqueIndex"

  <class name="AccountSummary" table="account_summaries">
    <id name="Id" column="id">
      <generator class="native"/>
    </id>
    <property name="TradeDate" column="trade_date" type="date" not-null="true" index="myUniqueIndex" />
    <many-to-one column="acount_number" lazy="false" index="myUniqueIndex">
  </class>

hope this helps

Upvotes: 1

Related Questions