如何解决如何在Android Room CrossReference实体中查询特殊的“多对多关系”用例的数据?
我按照android开发者指南定义了Room多对多关系: https://developer.android.com/training/data-storage/room/relationships#many-to-many
data class Playlist(
@PrimaryKey val playlistId: Long,val playlistName: String
)
@Entity
data class Song(
@PrimaryKey val songId: Long,val songName: String,val artist: String
)
@Entity(primaryKeys = ["playlistId","songId"])
data class PlaylistSongCrossRef(
val playlistId: Long,val songId: Long
)
data class PlaylistWithSongs(
@Embedded val playlist: Playlist,@Relation(
parentColumn = "playlistId",entityColumn = "songId",associateBy = @Junction(PlaylistSongCrossRef::class)
)
val songs: List<Song>
)
// Dao
@Transaction
@Query("SELECT * FROM Playlist")
fun getPlaylistsWithSongs(): List<PlaylistWithSongs>
以上工作正常。但是,我还想根据歌曲所属的播放列表保存歌曲的声音属性(即基本音量...)。对于同一首歌曲,在“聚会”播放列表中时音量较高,而在“冷却”播放列表中时音量较低。对于相同的播放列表,类似的情况是,某些歌曲的音量较高,其余歌曲的音量较低。
所以我更改了上面的PlaylistSongCrossRef并添加了以下内容:
@Entity(table="playlistSongCrossRef",primaryKeys = ["playlistId",val songId: Long,val baseVolume: Double
)
data class PlaylistWithSongs(
@Embedded val playlist: Playlist,associateBy = Junction(PlaylistSongCrossRef::class)
)
val songs: List<Song>
@Relation(
parentColumn = "playlistId",entityColumn = "baseVolume",associateBy = Junction(PlaylistSongCrossRef::class)
)
val baseVolumes: List<PlaylistSongCrossRef>
)
// Dao
@Query("SELECT * FROM Playlist WHERE playlistId = :playlistId ")
fun getPlaylistsWithSongs(playlistId: Int): List<PlaylistWithSongs>
但是,dao查询无法正常工作。当相同的baseVolume存储在不同的song-playlistCrossRef对中时,查询将返回重复的条目。
我不确定这是否是dao中的错误查询,还是我需要重新设计Room DB结构。提前非常感谢您!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。