gpt4 book ai didi

NHibernate 简单的一对多映射使用 NHibernate.Mapping.Attributes

转载 作者:行者123 更新时间:2023-12-05 03:16:02 24 4
gpt4 key购买 nike

谁能给我一个非常清楚地描述基于属性的简单一对多映射的网络链接?

例如(一家公司只能有一个国家,但一个国家有很多公司):

class Company
{
}

class Country
{
IList<Company> Items;
}

最佳答案

对于您的示例类,并为无序集合使用包:

using Map = NHibernate.Mapping.Attributes;

[Map.Class( 0, Table = "country", NameType=typeof(Country) )]
public class Country
{
[Map.Id( 1, Name = "Id" )]
[Map.Generator( 2, Class = "identity" )]
public virtual int Id { get; set; }
[Map.Property]
public virtual string Name { get; set; }
[Map.Bag( 0, Table = "country_company" )]
[Map.Key( 1, Column = "countryid" )]
[Map.OneToMany( 2, ClassType = typeof( Company ) )]
public virtual IList<Company> Items { get; set; }
}

[Map.Class( 0, Table = "country_company", NameType = typeof( Company ) )]
public class Company
{
[Map.Id( 1, Name = "Id" )]
[Map.Generator( 2, Class = "identity" )]
public virtual Guid Id { get; set; }
[Map.Property]
public virtual string Name { get; set; }
}

生成以下 hbm.xml:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="nh.Country, nh" table="country">
<id name="Id">
<generator class="identity" />
</id>
<property name="Name" />
<bag name="Items" table="country_company">
<key column="countryid" />
<one-to-many class="nh.Company, nh" />
</bag>
</class>
<class name="nh.Company, nh" table="country_company">
<id name="Id">
<generator class="identity" />
</id>
<property name="Name" />
</class>
</hibernate-mapping>

关于NHibernate 简单的一对多映射使用 NHibernate.Mapping.Attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1986472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com