如果我想关联 2 个表并且关系不仅是一列而是更多,有没有办法使用带有更多参数的 Room @Relation 注释来实现这一点?也许像下面这样
@Relation(parentColumn = "{message_id,account_id}", entityColumn = "{message_id, from_account_id}", entity = Message::class)
var messageList: List<Message> = ArrayList()
Run Code Online (Sandbox Code Playgroud)
所以连接有多个约束?
在我的 Android 应用程序中,我使用 Room 进行数据存储。我目前面临的问题是,我需要在嵌入对象的 @Relation 中放入 2 列,因为关系依赖于 2 列。结构见下图:
@Entity(tableName = "damages")
public class Damage {
@PrimaryKey(autoGenerate = true)
@NonNull
private Long id;
@NonNull
private Long projectId;
@NonNull
private Long codeId;
private String description;
...
}
@Entity(tableName = "damage_codes")
public class DamageCode {
@PrimaryKey(autoGenerate = true)
@NonNull
private Long id;
@NonNull
private Long projectId;
@NonNull
private String name;
private String description;
@NonNull
private String code;
@NonNull
private String damageType;
...
}
@Entity(tableName = "damage_types")
public class DamageType { …Run Code Online (Sandbox Code Playgroud) 我想向“CrossRef”表添加额外的字段。我的问题是如何让 DateWithSteaks 从“CrossRef”表中获取这个“isCompleted”变量?
@Entity
public class Steak{
@PrimaryKey(autoGenerate = true)
public int id;
public String title;
}
@Entity
public class Date {
@PrimaryKey
public long date;
public Date() {
date = new LocalDate().toDate().getTime();
}
}
@Entity(primaryKeys = {"date", "id"})
public class DateSteakCrossRef {
public long date;
public int id;
public boolean isCompleted;
}
public class DateWithSteaks {
@Embedded Date date;
@Relation(
parentColumn = "date",
entityColumn = "id",
associateBy = @Junction(DateSteakCrossRef.class)
)
public List<Steak> steaks;
}
Run Code Online (Sandbox Code Playgroud) 我有 2 个实体,并且Grade有一个通往教室的外键。嵌入式实体看起来像这样,StudentStudentClassRoom
data class ClassRoom(
@Embedded
val level: ClassLevel
@Relation(
parentColumn = "id",
entityColumn = "ClassLevel"
)
val students: List<Student>,
// val strength: Int ?
)
Run Code Online (Sandbox Code Playgroud)
我想要实现的是,使用ClassRoomInstance.strength来获取传入外键的计数。比计算学生的规模students.size
达到这个目的的方法是什么?
谢谢
我想删除一对多关系中的所有值
父表:
@Entity(tableName = "Product")
data class Products (
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "id") var id : Int = 0,
@ColumnInfo(name = "name")
var name : String? = null,
@ColumnInfo(name = "category_id")
var category_id : String? = null,
@ColumnInfo(name = "subcategory_id")
var subcategory_id : String? = null,
@ColumnInfo(name = "other_images")
var other_images: List<String> = listOf(),
@ColumnInfo(name = "price")
var price : String? = null,
@ColumnInfo(name = "variants")
var variants : List<Variants> = listOf()
)
Run Code Online (Sandbox Code Playgroud)
子表:
@Entity(tableName = "Variant")
data …Run Code Online (Sandbox Code Playgroud) 我试图在房间数据库 android 中添加一些数据。但是在添加时只有 50 行可见或输入到数据库中。数据库中无法添加更多数据。我还检查了我的日志,但没有错误或异常。我打印日志中的每个数据,它在日志中可见,但在数据库中不可用。创建我放置的房间数据库时没有任何限制。如果有人知道出了什么问题,请帮助我
database android android-studio android-room android-room-relation