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
?
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.