非主键字段与主键字段的关联,在类中实现了一对一的单向映射。订单充值业务。显然,一单位可以有多个充值信息。通过表 Deposit里面的CompanyId字段关联表Company主键Id字段。
订单信息
[Serializable] public class DepositInfo { /// <summary> /// 商户订单号 /// </summary> public virtual string TradeNo {get;set;} /// <summary> /// 商家Id /// </summary> public virtual int CompanyId {get;set;} public virtual Company CompanyInfo { get; set; } }
映射文件
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="Model.DepositInfo, Model " table="dbo.Deposit"> <id name="Id" type="Int32" unsaved-value="http://www.cnblogs.com/lucika/p/0"> <column name="Id" length="4" sql-type="int" not-null="true" unique="true"/> <generator class="native" /> </id> <property name="TradeNo" type="string"/> <property name="CompanyId" column="CompanyId" type="int" /> <many-to-one name="CompanyInfo" fetch="join" column="CompanyId" not-found="ignore"></many-to-one> </class> </hibernate-mapping>
商家信息
public class Company { public virtual int Id {get;set;} /// <summary> /// 厂家名称 /// </summary> public virtual string Name {get;set;} }
映射文件
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="Model.Company, Model" table="Company"> <id name="Id" column="Id" type="int" unsaved-value="http://www.cnblogs.com/lucika/p/0"> <generator class="native" /> </id> <property name="Name" column="Name" type="string" /> </class> </hibernate-mapping>