NHibernate映射问题

发布于 2024-11-26 03:26:41 字数 2067 浏览 3 评论 0 原文

我不知道如何使用 Nhibernate 3.0 获得正确的映射

我有两个 POCO:

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; }
}

我想映射表中的类,如下所示:

表:accounts(列:account_number)

表:account_summaries(列:id,column: trade_date,列:account_number)

如何避免在 AccountSummary 类中出现 Account 对象,就像我通常对一对多/多对一所做的那样映射。我希望 AccountSummary 类具有 AccountNumber 属性,而不是具有 Account 对象的 AccountNumber 属性,如类定义中那样,并引用 Account 类。

我还想将 trade_date 和 account_number 列映射为自然 ID 或唯一键。

我的映射如下所示:

<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>

任何形式的帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2024-12-03 03:26:41

这还不够吗?

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; }
}

和唯一约束请参阅“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>

希望这有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文