多个类包含的类的 NHibernate 映射

发布于 2024-10-28 00:00:06 字数 749 浏览 1 评论 0原文

我在 NHibernate 映射中遇到问题。 我有班级公司、人员和地址;公司和个人都可以有地址,因此我在两者中都取了地址。为了存储这个,我有表 Company、Person 和 Address。现在公司将有地址对象,个人也将有地址对象,因此地址也应该引用公司和个人对象。因此,我创建了 Address 1. CompanyAddress 2. PersonAddress 的两个子类,并在数据库中创建了另外两个表 Company_Address 和 Person_Address。现在,在 Address.hbm.xml 中,我为 CompanyAddress 和 PersonAddress 添加了 Joined 子类,它们分别引用 Company_Address 和 Person_Address 表。

现在 CompanyAddress 类中包含公司对象,而 PersonAddress 类中包含 Person 对象。

Company_Address 有 2 列 AddressId(PK) 和 CompanyId(FK)->Company Person_Address 有 2 列 AddressId(PK) 和 PersonId(FK)->Person

我已在 Company.hbm.xml 中为 Address 创建了一对一映射。 当我保存公司对象时,除了 Company_Address 之外,每个表都已正确填充。 AddressId 已存储,但 CompanyId 未存储。

我不知道如何让它工作

如果有人遇到这个问题请帮忙。

提前致谢!!! 帕万·舒克拉

I have a problem in NHibernate mapping.
I have Class Company, Person and Address; Company and Person both can have Addresses hence i have taken Address in both. To store this I have tables Company, Person and Address. Now Company will have Address object and Person will also have Address Object so Address should also have reference to Company and Person object. So I created two child classes of Address 1. CompanyAddress 2. PersonAddress and in Database I created two more tables Company_Address and Person_Address. Now In Address.hbm.xml i have added Joined subclass for both CompanyAddress and PersonAddress which are referring to Company_Address and Person_Address tables respectively.

Now CompanyAddress class is having company object in it and PersonAddress class is having Person object in it.

Company_Address is having 2 columns AddressId(PK) and CompanyId(FK)->Company
Person_Address is having 2 columns AddressId(PK) and PersonId(FK)->Person

I have created one-to-one mapping in Company.hbm.xml for Address.
When i Save Company object every table is populating properly except Company_Address.
AddressId is getting stored but CompanyId is not getting stored.

I have no idea how to get this working

If someone can faced this problem please help.

Thanks in advance!!!
Pawan Shukla

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

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

发布评论

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

评论(2

最终幸福 2024-11-04 00:00:06

考虑到您已经设置了一对一的映射,听起来您可能在这里过度标准化了。可能更容易(并且代码更清晰)的方法是将地址字段放置在 Company 和 Person 表本身中,然后设置一个简单的地址对象并将其视为组件。这是我的地址类:

public class StreetAddress
{
    public string CountryCode { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string StateCode { get; set; }
    public string PostalCode { get; set; }

    public StreetAddress()
    {
        // Constructor for NHibernate
    }

    public StreetAddress(string countryCode, string street, string city, string county, string stateCode, string postalCode)
    {
        CountryCode = countryCode;
        Street = street;
        City = city;
        County = county;
        StateCode = stateCode;
        PostalCode = postalCode;
    }
}

然后将地址视为一个组件,并像这样映射它:

<component name="Address" insert="true" update="true" optimistic-lock="true">
  <property name="CountryCode">
    <column name="Address_CountryCode" />
  </property>
  <property name="Street">
    <column name="Address_Street" />
  </property>
  <property name="City">
    <column name="Address_City" />
  </property>
  <property name="County">
    <column name="Address_County" />
  </property>
  <property name="StateCode">
    <column name="Address_StateCode" />
  </property>
  <property name="PostalCode">
    <column name="Address_PostalCode" />
  </property>
</component>

It sounds like you may have over-normalized here, given that you have set up one-to-one mappings. What might be easier (and WAY cleaner in code) is to place your address fields in the Company and Person tables themselves, then set up a simple address object and treat it as a component. Here's my address class:

public class StreetAddress
{
    public string CountryCode { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string StateCode { get; set; }
    public string PostalCode { get; set; }

    public StreetAddress()
    {
        // Constructor for NHibernate
    }

    public StreetAddress(string countryCode, string street, string city, string county, string stateCode, string postalCode)
    {
        CountryCode = countryCode;
        Street = street;
        City = city;
        County = county;
        StateCode = stateCode;
        PostalCode = postalCode;
    }
}

Then you treat the address as a component, and map it like this:

<component name="Address" insert="true" update="true" optimistic-lock="true">
  <property name="CountryCode">
    <column name="Address_CountryCode" />
  </property>
  <property name="Street">
    <column name="Address_Street" />
  </property>
  <property name="City">
    <column name="Address_City" />
  </property>
  <property name="County">
    <column name="Address_County" />
  </property>
  <property name="StateCode">
    <column name="Address_StateCode" />
  </property>
  <property name="PostalCode">
    <column name="Address_PostalCode" />
  </property>
</component>
暖伴 2024-11-04 00:00:06

不要这样做。地址显然不是一个实体(因此没有自己的表,也没有主键)。我宁愿做的是将其建模为一个组件。示例映射可能如下所示:

<class name="Company"
    table="Company">

    <id name="Id">
        <generator class="identity"/>
    </id>
    <property name="CompanyName" />
    <component name="Address">
        <property name="Street"/>
        <property name="HouseNumber"/>
        <property name="City"/>
        <property name="PostOffice"/>
    </component>
</class>

只需谷歌一下即可。在 DDD 中,有一个与实体相对的值对象的概念,在 NHibernate 中对值对象建模的方法是使用组件。

Don't do it this way. Address is clearly not an entity (and hence has no table of it's own and no primary key). What I would rather do is to model it as a component. A sample mapping might look like this:

<class name="Company"
    table="Company">

    <id name="Id">
        <generator class="identity"/>
    </id>
    <property name="CompanyName" />
    <component name="Address">
        <property name="Street"/>
        <property name="HouseNumber"/>
        <property name="City"/>
        <property name="PostOffice"/>
    </component>
</class>

Just google a bit. In DDD there's a notion of a value object opposed to an entity, and the way to model value objects in NHibernate is to use components.

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