In my Android App I am using Room for data storage. I am currently facing the problem, that I need to put 2 columns in my @Relation of the embedded object because the relation depends on 2 columns. See below the structure:
@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 {
@PrimaryKey(autoGenerate = true)
@NonNull
private Long id;
@NonNull
private Long projectId;
@NonNull
private String ttype;
private String description;
...
}
public class DamageWithCode {
@Embedded public Damage damage;
@Relation(
entity = DamageCode.class,
parentColumn = "codeId",
entityColumn = "id"
)
public DamageCodeAndType code;
public Damage getDamage() {
return damage;
}
public DamageCodeAndType getCode() {
return code;
}
}
public class DamageCodeAndType {
@Embedded public DamageCode code;
@Relation(
parentColumn = "damageType",
entityColumn = "ttype"
)
public DamageType damageType;
public DamageCode getCode() {
return code;
}
public DamageType getDamageType() {
return damageType;
}
}
In my DamageDAO I have the methods:
@Transaction
@Query("SELECT * FROM damages WHERE projectId=:projectId")
public List<DamageWithCode> getDamages(Long projectId);
@Query("SELECT * FROM damages WHERE id=:id")
public DamageWithCode getDamage(Long id);
The problem: In my DamageCodeAndType class in the @Relation I need to specify not only damageType as parentColumn but also the projectId. Additionally, in the entityColumn I need to add the projectId to the ttype column. How can I do that? Is something like this possible?
public class DamageCodeAndType {
@Embedded public DamageCode code;
@Relation(
parentColumn = {"damageType","projectId"},
entityColumn = {"ttype","projectId"}
)
public DamageType damageType;
public DamageCode getCode() {
return code;
}
public DamageType getDamageType() {
return damageType;
}
}
If this is not possible, can anybody point me in the direction how to solve this. Thank you.