我有一个 Rotation
类,其中包含对多个 Advert
对象列表的引用。我更喜欢一个实现,其中 Rotation
具有 List>
类型的属性来保存这些,但我无法提供支持的 NHibernate 映射这。
在数据库架构中,Rotation
和 Advert
之间的多对多关系表示为包含以下列的 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
?
发布评论
评论(3)
我今天刚刚遇到了同样的问题。阅读 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:
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.
我能想到的最好的办法就是使模型适应所需的数据库结构。
映射:
数据库:
然后您可以轻松创建如下属性:
The best I can think of is to adapt the model to the desired database structure.
mapping:
Database:
Then you can easily create properties like this:
我建议有一个旋转类,其中包含广告列表。然后,广告保存父子关系中的子广告列表。
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.