如何映射 List>在NHibernate中?

发布于 2024-08-17 08:53:37 字数 1361 浏览 9 评论 0 原文

我有一个 Rotation 类,其中包含对多个 Advert 对象列表的引用。我更喜欢一个实现,其中 Rotation 具有 List> 类型的属性来保存这些,但我无法提供支持的 NHibernate 映射这。

在数据库架构中,RotationAdvert 之间的多对多关系表示为包含以下列的 RotationAdvert 表:

  • RotationID
  • AdvertID
  • Variant(“水平位置”/外部列表中的索引)
  • Position(“垂直位置”/内部列表中的索引 。

我发现的最佳解决方案是在 Rotation 上实现固定数量的 List 类型属性,并使用 扩展映射 list> 每个元素:

<list name="Variant1" table="RotationAdvert" where="Variant = 1">
    <key column="RotationID"/>
    <index column="Position"/>
    <many-to-many class="Advert" column="AdvertID"/>
</list>

<list name="Variant2" table="RotationAdvert" where="Variant = 2">
    <key column="RotationID"/>
    <index column="Position"/>
    <many-to-many class="Advert" column="AdvertID"/>
</list>

etc...

但是,这要求我指定固定数量的变体,我真的想避免这种情况。

我还有哪些其他选择?我可以将 RotationVariant 类压缩到模型中 - 无需在数据库中创建新表 - 并以某种方式在 Rotation 上映射 List 属性?或者我是否必须在数据库中创建一个新表,只为每个 RotationVariant 保存一个 ID?

I have a Rotation class, which contains references to multiple lists of Advert objects. I would prefer an implementation where Rotation has a property of type List<List<Advert>> to hold these, but I am unable to come up with an NHibernate mapping supporting this.

In the database schema, the many-to-many relation between Rotation and Advert is represented as a table RotationAdvert with the following columns:

  • RotationID
  • AdvertID
  • Variant ("horizontal position" / index within outer list)
  • Position ("vertical position" / index within inner list)

The best solution I have found yet, is to implement a fixed number of List<Advert> typed properties on Rotation and extend the mapping with a <list> element for each:

<list name="Variant1" table="RotationAdvert" where="Variant = 1">
    <key column="RotationID"/>
    <index column="Position"/>
    <many-to-many class="Advert" column="AdvertID"/>
</list>

<list name="Variant2" table="RotationAdvert" where="Variant = 2">
    <key column="RotationID"/>
    <index column="Position"/>
    <many-to-many class="Advert" column="AdvertID"/>
</list>

etc...

However, this requires me to specify a fixed number of variants, which I really would like to avoid.

What are my other options? Can I squeeze a RotationVariant class into the model - without creating new tables in the database - and somehow map a List<RotationVariant> property on Rotation? Or will I have to create a new table in the database, just to hold an ID for each RotationVariant?

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

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

发布评论

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

评论(3

记忆里有你的影子 2024-08-24 08:53:37

我今天刚刚遇到了同样的问题。阅读 Hibernate 文档并在第 6.1 节找到答案:集合映射 https:// /www.hibernate.org/hib_docs/nhibernate/html/collections.html

集合不能包含其他集合

我在 NHibernate 文档中找不到这个确切的句子,但似乎同样的规则适用。正如斯蒂芬和克里斯所说,您可能需要另一个实体来保存这些值。

I just ran into this same problem today. Reading through the Hibernate documentation and found an answer in section 6.1: Collection Mapping https://www.hibernate.org/hib_docs/nhibernate/html/collections.html:

Collections may not contain other collections

I couldn't find this exact sentence in the NHibernate docs, but it seems that the same rule applies. Like Stephan and Chris said, you will probably need to have another entity to hold the values.

笔芯 2024-08-24 08:53:37

我能想到的最好的办法就是使模型适应所需的数据库结构。

class Rotation
{
  IList<AdvertVariant> AdvertVariants { get; private set; }
}


class AdvertVariant
{
  int Variant { get; set; }
  Advert Advert { get; set; }
}

映射:

<class name="Rotation">
  <list name="AdvertVariants" table="RotationAdvert" >
    <key column="RotationID"/>
    <index column="Position"/>
    <one-to-many class="AdvertVariant"/>
  </list>
</class>

<class name="AdvertVariant">
  <property name="Variant" />
  <many-to-one name="Advert" column="VariantId"/>
</class>

数据库:

Rotation:
  id

AdvertVariant
  id
  Variant
  RotationId
  VariantId

Advert
  id

然后您可以轻松创建如下属性:

class Rotation
{
  //...
  IDictionary<int, IList<Adverts>> AdvertsByVariant
  {
    return VariantAdverts.ToDictionary(x => x.Variant, y => y.Advert);
  }
}

The best I can think of is to adapt the model to the desired database structure.

class Rotation
{
  IList<AdvertVariant> AdvertVariants { get; private set; }
}


class AdvertVariant
{
  int Variant { get; set; }
  Advert Advert { get; set; }
}

mapping:

<class name="Rotation">
  <list name="AdvertVariants" table="RotationAdvert" >
    <key column="RotationID"/>
    <index column="Position"/>
    <one-to-many class="AdvertVariant"/>
  </list>
</class>

<class name="AdvertVariant">
  <property name="Variant" />
  <many-to-one name="Advert" column="VariantId"/>
</class>

Database:

Rotation:
  id

AdvertVariant
  id
  Variant
  RotationId
  VariantId

Advert
  id

Then you can easily create properties like this:

class Rotation
{
  //...
  IDictionary<int, IList<Adverts>> AdvertsByVariant
  {
    return VariantAdverts.ToDictionary(x => x.Variant, y => y.Advert);
  }
}
多情癖 2024-08-24 08:53:37

我建议有一个旋转类,其中包含广告列表。然后,广告保存父子关系中的子广告列表。

I'd propose to have a Rotation class which holds a list of Adverts. An advert then holds a list of child adverts in a parent child relationship.

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