一对一的 NHibernate 映射,其中所有字段都映射到单个类中的属性

发布于 2024-09-30 19:20:27 字数 604 浏览 0 评论 0原文

我遇到的情况是,数据库在两个表之间具有一对一的关系,这两个表一起清楚地表示相同的逻辑实体。

我想将这些表映射到同一个类,并将类的属性映射到两个表中的字段。 (IE 而不是让一个类组成另一个类)。

因此,如果表格如下:

Table Foo 列:ID (PK)、描述、FavouriteCrispFlavour

表格栏 列:Id (PK)、NumberOfArms、EyeColour

我希望我的类如下所示:

public class FooBar
{
  public virtual int Id {get;set;)
  public virtual string Description {get;set;)
  public virtual CrispFlavour FavouriteCrispFlavour {get;set;)
  public virtual int NumberOfArms {get;set;)
  public virtual EyeColour EyeColour {get;set;)
}

这种映射在 NHibernate 中可能吗?

I have a situation where the database has a one-to-one relationship between two tables which together clearly represent the same logical entity.

I would like to map these tables to the same class, and have properties of the class map to fields from BOTH tables. (I.E. rather than have one class compose another).

So if the tables are as follows:

Table Foo
Columns: Id (PK), Description, FavouriteCrispFlavour

Table Bar
Columns: Id (PK), NumberOfArms, EyeColour

I would like my class to look like the following:

public class FooBar
{
  public virtual int Id {get;set;)
  public virtual string Description {get;set;)
  public virtual CrispFlavour FavouriteCrispFlavour {get;set;)
  public virtual int NumberOfArms {get;set;)
  public virtual EyeColour EyeColour {get;set;)
}

Is this mapping possible in NHibernate?

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

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

发布评论

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

评论(2

趁微风不噪 2024-10-07 19:20:27

是的,这称为连接映射。您可以使用带有 table 属性的 元素来从多个表组成一个实体,例如:

<class name="MyClass">
  ...
  <join table="otherTable">
    <key column="MyClass_Id"/>
    <property name="propInOtherTable" />
  </join>
</class>

有关完整(和更好)的描述,请参阅 NHibernate 文档 连接映射

Yes, it's called a join mapping. You use the <join> element with a table attribute to compose an entity from multiple tables, for example:

<class name="MyClass">
  ...
  <join table="otherTable">
    <key column="MyClass_Id"/>
    <property name="propInOtherTable" />
  </join>
</class>

For a full (and better) description, see the NHibernate documentation on join mappings.

伴我老 2024-10-07 19:20:27

是的,我相信您只需要在 HQL 中指定联接即可。

Yes, I believe you just need to specify the join in the HQL.

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