我在Room使用Relation添加了一对多的关系.我在这篇文章中提到了在Room中为关系编写以下代码.
该帖子告诉如何从数据库中读取值,但将实体存储到数据库中导致userId为空,这意味着两个表之间没有关系.
我不知道什么是理想的途径,insert一个User并List of Pet到数据库中,同时具有userId价值.
1)用户实体:
@Entity
public class User {
@PrimaryKey
public int id; // User id
}
Run Code Online (Sandbox Code Playgroud)
2)宠物实体:
@Entity
public class Pet {
@PrimaryKey
public int id; // Pet id
public int userId; // User id
public String name;
}
Run Code Online (Sandbox Code Playgroud)
3)UserWithPets POJO:
// Note: No annotation required at this class definition.
public class UserWithPets {
@Embedded
public User user;
@Relation(parentColumn = "id", …Run Code Online (Sandbox Code Playgroud) 我怎样才能代表与房间的多对多关系?例如,我有"访客"和"预订".预订可以有很多客人,客人可以参加许多预订.
这是我的实体定义:
@Entity data class Reservation(
@PrimaryKey val id: Long,
val table: String,
val guests: List<Guest>
)
@Entity data class Guest(
@PrimaryKey val id: Long,
val name: String,
val email: String
)
Run Code Online (Sandbox Code Playgroud)
在调查我遇到的文档时@Relation.我发现它真的很混乱.
根据这个,我想创建一个POJO并在那里添加关系.所以,以我的例子,我做了以下
data class ReservationForGuest(
@Embedded val reservation: Reservation,
@Relation(
parentColumn = "reservation.id",
entityColumn = "id",
entity = Guest::class
) val guestList: List<Guest>
)
Run Code Online (Sandbox Code Playgroud)
上面我得到编译器错误:
无法弄清楚如何从游标中读取此字段.
我无法找到工作样本@Relation.
在Android房间持久化库中如何将整个Model对象插入到本身具有另一个列表的表中.
让我告诉你我的意思:
@Entity(tableName = TABLE_NAME)
public class CountryModel {
public static final String TABLE_NAME = "Countries";
@PrimaryKey
private int idCountry;
private List<CountryLang> countryLang = null;
public int getIdCountry() {
return idCountry;
}
public void setIdCountry(int idCountry) {
this.idCountry = idCountry;
}
public String getIsoCode() {
return isoCode;
}
public void setIsoCode(String isoCode) {
this.isoCode = isoCode;
}
/**
here i am providing a list of coutry information how to insert
this into db along with CountryModel at same …Run Code Online (Sandbox Code Playgroud) 我刚刚开始与Room合作,虽然一切看起来都非常直观,但我目前还不清楚我究竟能如何处理关系.
由于SQLite是关系数据库,因此您可以指定对象之间的关系.尽管大多数ORM库允许实体对象相互引用,但Room明确禁止这样做.即使您不能使用直接关系,Room仍然允许您在实体之间定义外键约束.(来源:https://developer.android.com/topic/libraries/architecture/room.html#no-object-references)
基于我之前的问题(Android持久性空间:"无法弄清楚如何从光标读取此字段")我得到了工作,感谢反馈,我在Kolin中实现了相同的示例(请参阅下面的代码).我不得不做一些小改动,比如现在传递给查询的参数必须作为"p0","p1"等传递.现在在Kotlin中我得到以下与UserWithPets类相关的错误:
错误:无法弄清楚如何从游标中读取此字段.e:private java.util.List pets;
@Dao
interface UserDAO {
@get:Query("SELECT * FROM user")
val all: LiveData<List<User>>
@Insert
fun insertUser(user: User) //single one
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertUsers(vararg users: User)
@Query("SELECT * FROM User")
fun loadUsersWithPets(): LiveData<List<UserWithPets>>
}
@Entity
class Pet( var name: String?, var ownerId: Int,@PrimaryKey(autoGenerate = true)var id:Int)
@Dao
interface PetDAO {
@Query("SELECT * FROM pet")
val all: List<Pet>
@Query("SELECT * FROM pet WHERE id IN (:p0)")
fun loadAllByIds(petIds: IntArray): List<Pet>
@Insert
fun insert(pet: Pet)
@Insert
fun insertAll(vararg …Run Code Online (Sandbox Code Playgroud) 我有一个 VideoList 对象,我想使用房间库保存它,但是当我尝试将 @Embedded 与 public List list = null; 一起使用时 它给我以下错误: 错误:(23, 24) 错误:无法弄清楚如何将此字段保存到数据库中。您可以考虑为其添加类型转换器。
VideoList类如下。
@Entity
public class VideoList {
@PrimaryKey
public String id;
public String title;
public String viewType;
public Integer sortingOrder = null;
public String componentSlug;
public String endPoint = null;
@Embedded
public List<Video> list = null;
public boolean hidden = false; }
Any suggestions?
Run Code Online (Sandbox Code Playgroud) 请耐心等待,我对架构组件和 Android 总体来说是新手。我的问题与这个问题类似,但不幸的是接受的答案似乎不起作用。我有一个像这个答案一样的一对多关系的例子 。我的示例数据库有两个表 USERS 和 PETS,如下图所示:


假设我想要获取一个用户列表,其中包含按用户 ID 分组的宠物列表(仅限5 岁以下的宠物)。结果应如下所示(伪代码):
{uId:2,[Pet3,Pet4];uId: 4, [Pet6, Pet7];}
另一个要求是 Dao 需要将列表作为 LiveData 对象返回,因为我使用 MVVM 架构并希望它具有生命周期感知和可观察性。
满足这些要求,UserDao将如下所示:
@Dao
interface UserDao {
@Insert
void insert(User user);
@Transaction
@Query("SELECT USERS.uId, PETS.pId , PETS.userId, PETS.age " +
"FROM USERS INNER JOIN PETS ON PETS.userId = USERS.uId " +
"WHERE PETS.age < 5 " +
"GROUP BY USERS.uId")
LiveData<List<UserWithPets>> getUserPets();
}
Run Code Online (Sandbox Code Playgroud)
用户实体:
@Entity
public class User {
@PrimaryKey
public …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 android 应用程序,并在 Android 操作系统中使用新的架构组件:LiveData、ViewModel 和 Room。我对 Room 实现有一个关于创建 @Relation 的小问题,它返回 JOIN 查询(多对多关系)的结果。
我的数据库结构如下所示:
@Entity
public class Student{
@PrimaryKey
private int id;
private String name;
private String email;
}
@Entity
public class Group{
@PrimaryKey
private int id;
private String name;
}
@Entity(foreignKeys = {
@ForeignKey(entity = Student.class,
parentColumns = "id",
childColumns = "student_id"),
@ForeignKey(entity = Group.class,
parentColumns = "id",
childColumns = "group_id")
})
public class StudentGroup{
private int studentId;
private int groupId;
}
Run Code Online (Sandbox Code Playgroud)
我如何才能为特定学生获取所有组,像这样?
public class StudentWithGroups{
@Relation(parentColumn = "id", …Run Code Online (Sandbox Code Playgroud) android ×8
android-room ×7
android-architecture-components ×3
database ×2
kotlin ×2
sqlite ×2
java ×1
many-to-many ×1
orm ×1
pojo ×1
relation ×1