Android Room: apply multiple columns in relation of embedded object

烈酒焚心 提交于 2021-02-10 18:16:14

问题


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.

来源:https://stackoverflow.com/questions/65771353/android-room-apply-multiple-columns-in-relation-of-embedded-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!