我无法弄清楚如何使用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; }
}
我想在这样的表中映射类:
表:帐户(列:account_number)
表:account_summaries(列:id,列:trade_date,列:account_number)
如何避免在AccountSummary类中使用Account对象,就像我通常使用一对多/多对一映射一样。而不是我的AccountSummary类具有Account对象,我希望AccountSummary类具有AccountNumber属性,如在类定义中引用Account类。
我还想将trade_date和account_number列映射为natural-id或unique-key。
我的映射看起来像这样:
<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>
任何形式的帮助都将受到赞赏。
答案 0 :(得分:1)
这不够吗?
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>
希望这会有所帮助