我的音乐相关数据库中有三个实体:视频,相册和流派。视频和专辑都可以有多种类型,因此我尝试将它们与多对多关联链接起来。 我在DB中有什么:
Videos:
Id (int)
... (many other columns)
Albums:
Id (int)
... (many other columns)
Genre:
Id (int)
Name (string)
Item_Genres: (table we are mapping many-to-many with)
Id (int)
objectType (here I have "album" for albums and "video" for videos)
objectId (id from either Albums or Videos table)
genreId (Id of the appropriate genre)
我可以使用多对多来将类型映射到视频和专辑,但我需要获取所有类型特别视频。 我怎么能这样做?
目前我在视频映射中有这个:
<set name="Genres" table="item_genres" lazy="true">
<key>
<column name="objectId" not-null="true"/>
</key>
<many-to-many class="Repositories.Entities.Genre">
<column name="genreId" not-null="true"/>
</many-to-many>
</set>
当要求 video.Genres 时,我有该视频的类型和具有相同ID的相册。
答案 0 :(得分:1)
您可以尝试像这样定义视频映射:
<set name="Genres" table="item_genres" lazy="true" where="objectType = 'video'">
<key>
<column name="objectId" not-null="true"/>
</key>
<many-to-many class="Repositories.Entities.Genre">
<column name="genreId" not-null="true"/>
</many-to-many>
</set>
where =“objectType ='video'应确保您只获取视频而非专辑。