问题描述
我按照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 (将#修改为@)