Android:房间库中的多对多关系(sqlite)
Android: Many to Many relation in Room library(sqllite)
我有一个媒体播放器应用程序,其中 "Song" 作为实体,"Playlist" 作为实体。每首歌曲可以在多个播放列表中,每个播放列表包含多首歌曲。我正在使用 Room 库作为我的 sqlite 数据库管理器。有什么建议如何实现这种多对多关系?我没有在文档中找到如何在 Room 上实现多对多关系的示例。
您需要创建 3 个 table:
歌曲(song_id,song_name,艺术家)
播放列表 (playlist_id,playlist_name)
一个路口table (playlist_id ,song_id )
参考链接:
您需要创建一个新的 "junction" class 来表示 "belongs to" 关系。在房间里,它会是这样的:
@Entity(tableName = "playlist_entries",
primaryKeys = { "songId", "playlistId" },
foreignKeys = {
@ForeignKey(entity = Song.class,
parentColumns = "id",
childColumns = "songId"),
@ForeignKey(entity = Playlist.class,
parentColumns = "id",
childColumns = "playlistId")
})
public class PlaylistEntry {
public final int songId;
public final int playlistId;
public PlaylistEntry(final int songId, final int playlistId {
this.songId = songId;
this.playlistId = playlistId;
}
}
在 Room 中实现多对多关系与在任何 SQL 数据库中实现多对多关系没有什么不同:有一个连接 table。对于 Room,连接 table 是通过连接 @Entity
.
创建的
This sample project from this book 说明了该技术。
我有一个 Category
class,恰好实现了树结构(父子类别之间的 1:N 关系):
@Entity(
tableName="categories",
foreignKeys=@ForeignKey(
entity=Category.class,
parentColumns="id",
childColumns="parentId",
onDelete=CASCADE),
indices=@Index(value="parentId"))
public class Category {
@PrimaryKey
@NonNull
public final String id;
public final String title;
public final String parentId;
@Ignore
public Category(String title) {
this(title, null);
}
@Ignore
public Category(String title, String parentId) {
this(UUID.randomUUID().toString(), title, parentId);
}
public Category(String id, String title, String parentId) {
this.id=id;
this.title=title;
this.parentId=parentId;
}
}
而且,我有一个 Customer
,嵌套 CategoryJoin
来表示客户和类别之间的 M:N 关系:
@Entity(indices={@Index(value="postalCode", unique=true)})
class Customer {
@PrimaryKey
@NonNull
public final String id;
public final String postalCode;
public final String displayName;
public final Date creationDate;
@Embedded
public final LocationColumns officeLocation;
public final Set<String> tags;
@Ignore
Customer(String postalCode, String displayName, LocationColumns officeLocation,
Set<String> tags) {
this(UUID.randomUUID().toString(), postalCode, displayName, new Date(),
officeLocation, tags);
}
Customer(String id, String postalCode, String displayName, Date creationDate,
LocationColumns officeLocation, Set<String> tags) {
this.id=id;
this.postalCode=postalCode;
this.displayName=displayName;
this.creationDate=creationDate;
this.officeLocation=officeLocation;
this.tags=tags;
}
@Entity(
tableName="customer_category_join",
primaryKeys={"categoryId", "customerId"},
foreignKeys={
@ForeignKey(
entity=Category.class,
parentColumns="id",
childColumns="categoryId",
onDelete=CASCADE),
@ForeignKey(
entity=Customer.class,
parentColumns="id",
childColumns="customerId",
onDelete=CASCADE)},
indices={
@Index(value="categoryId"),
@Index(value="customerId")
}
)
public static class CategoryJoin {
@NonNull public final String categoryId;
@NonNull public final String customerId;
public CategoryJoin(String categoryId, String customerId) {
this.categoryId=categoryId;
this.customerId=customerId;
}
}
}
您的 @Dao
然后具有根据关系检索对象的方法,例如:
@Query("SELECT categories.* FROM categories\n"+
"INNER JOIN customer_category_join ON categories.id=customer_category_join.categoryId\n"+
"WHERE customer_category_join.customerId=:customerId")
List<Category> categoriesForCustomer(String customerId);
@Query("SELECT Customer.* FROM Customer\n"+
"INNER JOIN customer_category_join ON Customer.id=customer_category_join.customerId\n"+
"WHERE customer_category_join.categoryId=:categoryId")
List<Customer> customersForCategory(String categoryId);
我有一个媒体播放器应用程序,其中 "Song" 作为实体,"Playlist" 作为实体。每首歌曲可以在多个播放列表中,每个播放列表包含多首歌曲。我正在使用 Room 库作为我的 sqlite 数据库管理器。有什么建议如何实现这种多对多关系?我没有在文档中找到如何在 Room 上实现多对多关系的示例。
您需要创建 3 个 table:
歌曲(song_id,song_name,艺术家)
播放列表 (playlist_id,playlist_name)
一个路口table (playlist_id ,song_id )
参考链接:
您需要创建一个新的 "junction" class 来表示 "belongs to" 关系。在房间里,它会是这样的:
@Entity(tableName = "playlist_entries",
primaryKeys = { "songId", "playlistId" },
foreignKeys = {
@ForeignKey(entity = Song.class,
parentColumns = "id",
childColumns = "songId"),
@ForeignKey(entity = Playlist.class,
parentColumns = "id",
childColumns = "playlistId")
})
public class PlaylistEntry {
public final int songId;
public final int playlistId;
public PlaylistEntry(final int songId, final int playlistId {
this.songId = songId;
this.playlistId = playlistId;
}
}
在 Room 中实现多对多关系与在任何 SQL 数据库中实现多对多关系没有什么不同:有一个连接 table。对于 Room,连接 table 是通过连接 @Entity
.
This sample project from this book 说明了该技术。
我有一个 Category
class,恰好实现了树结构(父子类别之间的 1:N 关系):
@Entity(
tableName="categories",
foreignKeys=@ForeignKey(
entity=Category.class,
parentColumns="id",
childColumns="parentId",
onDelete=CASCADE),
indices=@Index(value="parentId"))
public class Category {
@PrimaryKey
@NonNull
public final String id;
public final String title;
public final String parentId;
@Ignore
public Category(String title) {
this(title, null);
}
@Ignore
public Category(String title, String parentId) {
this(UUID.randomUUID().toString(), title, parentId);
}
public Category(String id, String title, String parentId) {
this.id=id;
this.title=title;
this.parentId=parentId;
}
}
而且,我有一个 Customer
,嵌套 CategoryJoin
来表示客户和类别之间的 M:N 关系:
@Entity(indices={@Index(value="postalCode", unique=true)})
class Customer {
@PrimaryKey
@NonNull
public final String id;
public final String postalCode;
public final String displayName;
public final Date creationDate;
@Embedded
public final LocationColumns officeLocation;
public final Set<String> tags;
@Ignore
Customer(String postalCode, String displayName, LocationColumns officeLocation,
Set<String> tags) {
this(UUID.randomUUID().toString(), postalCode, displayName, new Date(),
officeLocation, tags);
}
Customer(String id, String postalCode, String displayName, Date creationDate,
LocationColumns officeLocation, Set<String> tags) {
this.id=id;
this.postalCode=postalCode;
this.displayName=displayName;
this.creationDate=creationDate;
this.officeLocation=officeLocation;
this.tags=tags;
}
@Entity(
tableName="customer_category_join",
primaryKeys={"categoryId", "customerId"},
foreignKeys={
@ForeignKey(
entity=Category.class,
parentColumns="id",
childColumns="categoryId",
onDelete=CASCADE),
@ForeignKey(
entity=Customer.class,
parentColumns="id",
childColumns="customerId",
onDelete=CASCADE)},
indices={
@Index(value="categoryId"),
@Index(value="customerId")
}
)
public static class CategoryJoin {
@NonNull public final String categoryId;
@NonNull public final String customerId;
public CategoryJoin(String categoryId, String customerId) {
this.categoryId=categoryId;
this.customerId=customerId;
}
}
}
您的 @Dao
然后具有根据关系检索对象的方法,例如:
@Query("SELECT categories.* FROM categories\n"+
"INNER JOIN customer_category_join ON categories.id=customer_category_join.categoryId\n"+
"WHERE customer_category_join.customerId=:customerId")
List<Category> categoriesForCustomer(String customerId);
@Query("SELECT Customer.* FROM Customer\n"+
"INNER JOIN customer_category_join ON Customer.id=customer_category_join.customerId\n"+
"WHERE customer_category_join.categoryId=:categoryId")
List<Customer> customersForCategory(String categoryId);